赞
踩
import sys
import getopt
# 使用sys.argv获取到的参数第一个为脚本的名字,如果不是,执行就会报错
# 我们使用getopt解析是一般都会使用sys.argv[1:]过滤掉第一个参数, 即过滤掉脚本的名字
argv = sys.argv
def getopt(args, shortopts, longopts = [])
sys.argv[1:]
,即执行python脚本时传入的过滤掉脚本名的参数内容-h
",后面不加带附加参数,分析串中需要写上"h
“或 “-h
”,例如 “-o file
”, 后面带有附加参数的情况,分析串中要写成”o:
" 或者"-o:
"--help
",后面不跟"=", 分析串列表中需要加入"["help"]
", 例如 --output=out
,后面带有"=",分析串列表中需要加入"[output=]
"shortopts
,longopts
值或者调用python脚本的参数顺序不对时会发生异常一.正常操作
import sys
import getopt
if __name__ == '__main__':
opts, args = getopt.getopt(sys.argv[1:], "ho:", ["help", "output="])
print("opts: " + opts)
print("args: " + args)
python test.py -h -o file --help --output=out file1 file2
>>> [('-h', ''), ('-o', 'file'), ('--help', ''), ('--output', 'out')]
>>> ['file1', 'file2']
import sys
import gtopt
if __name__ == '__main__':
opts, args = getopt.getopt(sys.argv[1:], "hto:", ["help", "test", "output="])
print(opts)
print(args)
python test.py -h -o file --help --output=out file1 file2
>>> [('-h', ''), ('-o', 'file'), ('--help', ''), ('--output', 'out')]
>>> ['file1', 'file2']
二.异常操作
调用python脚本时输入带有-
或者--
类型的参数在getopt函数调用时没有写入
Code
import sys
import getopt
if __name__ == '__main__':
opts, args = getopt.getopt(sys.argv[1:], "ho:", ["help", "output="])
print("opts: " + opts)
print("args: " + args)
Output
会导致异常
python test.py -h -t -o file --help --output=out file1 file2
>>> Traceback (most recent call last):
>>> File "test.py", line 5, in <module>
>>> opts, args = getopt.getopt(sys.argv[1:], "ho:", ["help", "output="])
>>> 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 195, in do_shorts
>>> if short_has_arg(opt, shortopts):
>>> File "C:\Python37\lib\getopt.py", line 211, in short_has_arg
>>> raise GetoptError(_('option -%s not recognized') % opt, opt)
>>> getopt.GetoptError: option -t not recognized
getopt函数调用中传入的参数和调用python脚本时传入的参数类型不匹配,例如脚本调用时传入的是"-o file
", getopt函数调用时使用"o
“而不是”o:
"
Code
import sys
import getopt
if __name__ == '__main__':
opts, args = getopt.getopt(sys.argv[1:], "ho", ["help", "output="])
print(opts)
print(args)
Output
打印出的opts和args的值不符合预期
python test.py -h -o file --help --output=out file1 file2
>>> [('-h', ''), ('-o', '')]
>>> ['file', '--help', '--output=out', 'file1', 'file2']
调用python脚本时调用时带有-
或者--
类型的参数放到了其它类型参数之后
Code
import sys
import getopt
if __name__ == '__main__':
opts, args = getopt.getopt(sys.argv[1:], "ho:", ["help", "output="])
print(opts)
print(args)
Output
打印出的opts和args的值不符合预期, "-o
"参数未解析到
python test.py -h --help --output=out file1 file2 -o file
>>> [('-h', ''), ('--help', ''), ('--output', 'out')]
>>> ['file1', 'file2', '-o', 'file']
在使用getopt函数处理之后,会得到两个opts和args两个数组,我们可以通过for循环的方式来获取用户传入的值
import sys
import getopt
if __name__ == '__main__':
try:
opts, args = getopt.getopt(sys.argv[1:], "ho:", ["help", "output="])
print(opts)
print(args)
for option, value in opts:
print(f"option: {option}, value: {value}")
except Exception as e:
print(f"Error: {e}")
python test.py -h -o file --help --output=out file1 file2
>>> [('-h', ''), ('-o', 'file'), ('--help', ''), ('--output', 'out')]
>>> ['file1', 'file2']
>>> option: -h, value:
>>> option: -o, value: file
>>> option: --help, value:
>>> option: --output, value: out
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。