1. 程式人生 > >Python解析Linux命令行

Python解析Linux命令行

get 選項 python lan ref nbsp pytho cmd target

寫了個python腳本在linux需要傳入參數使用,python參數傳入有幾個方法,

先用了Python中命令行參數的最傳統的方法sys.argv

linux cmd

~& python main.py --all haha

python code:main.py

import sys

info1 = sys.argv[1]
info2 = sys.argv[2]

print(info1,type(info1))
print(info2,type(info2))

# output 
--all <class ‘str‘>
haha <class ‘str‘>

這個方法適用於小腳本和少點的參數時用。

Python中還內置了一個用於命令項選項與參數解析的模塊argparse,這個簡單且強大。

見https://blog.csdn.net/Quincuntial/article/details/77963301

Python解析Linux命令行