Python解析Linux命令行

写了个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

猜你喜欢

转载自www.cnblogs.com/kumata/p/10231116.html