欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > python >内容正文

python

python getopterror_python3 getopt用法

发布时间:2023/12/9 python 47 豆豆
生活随笔 收集整理的这篇文章主要介绍了 python getopterror_python3 getopt用法 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

python channel_builder.py -s /Users/graypn/ -d /Users/graypn/Documents -m 7 --out=report/xx.html

参数也分长格式和短格式

短格式:-s

长格式:--source

opts, args = getopt.getopt(sys.argv[1:], "hs:d:m:v:p:c:",

["help", "src=", "dst=", "major=", "version=", "platform=", "channels="])

这个 opts 是一个字典类型,getopt.getopt(x,y,z) x函数第一个传入参sys.argv[1:],y第二个传段short短数名"hs:d:m:v:p:c:",z第三个是long长参数名["help", "src=", "dst=", "major=", "version=", "platform=", "channels="]

short 用法就是 python test.py -h 111

long用法可以2种 --out=res/tt.html

或者直接 --out res/tt.html

关于第二个参数初学者会很困惑这啥意思:

其实是这样的单独一个字母表示该参数不带值,一个字母加个:比如p:表示该参数后面带值,"hf:-v"也可以写成'-h-f:-v',到底什么意思呢:

C:\Users\Administrator\PycharmProjects\checkTest\src>python xx.py -h

this is --help

C:\Users\Administrator\PycharmProjects\checkTest\src>python xx.py -f

Traceback (most recent call last):

File "xx.py", line 22, in

opts, args = getopt.getopt(sys.argv[1:], '-hf:-v', ['help', 'filename=', 'version'])

File "C:\Python37\lib\getopt.py", line 95, in getopt

opts, args = do_shorts(opts, args[0][1:], shortopts, args[1:])

File "C:\Python37\lib\getopt.py", line 199, in do_shorts

opt)

getopt.GetoptError: option -f requires argument

C:\Users\Administrator\PycharmProjects\checkTest\src>python xx.py -h

this is --help

C:\Users\Administrator\PycharmProjects\checkTest\src>python xx.py -f=aaaa.txt

[*] Filename is =aaaa.txt

意思就是你短参数不需要给我指定值,给我我也没发接受处理比如1000短参数给的值1000也没用,长参数是你必须给我值我才可以进行传参

总结

以上是生活随笔为你收集整理的python getopterror_python3 getopt用法的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。