转自:http://yiyunyaya.blog.163.com/blog/static/1654496362013266848952/
如果想对python脚本传参数,python中对应的argc, argv(c语言的命令行参数)是什么呢?
需要模块:sys
参数个数:len(sys.argv)脚本名: sys.argv[0]
参数1: sys.argv[1]
参数2: sys.argv[2]
test.py
1 |
import sys |
2 |
print "脚本名:" , 0 ] |
3 |
for i in range ( 1 , len (sys.argv)): |
4 |
print "参数" , |
>>>python test.py hello world
脚本名:test.py
参数 1 hello
参数 2 world
python中使用命令行选项:
例如我们需要一个convert.py脚本。它的作用是处理一个文件,并将处理后的结果输出到另一个文件中。
要求该脚本满足以下条件:
1.通过-i -o选项来区别参数是输入文件还是输出文件.
>>> python convert.py -i inputfile -o outputfile
2.当不知道convert.py需要哪些参数时,用-h打印出帮助信息
>>> python convert.py -h
getopt函数原形:
getopt.getopt(args, options[, long_options])
convert.py
import sys, |
02 |
03 |
opts, = getopt.getopt(sys.argv[ 1 :], "hi:o:" ) |
04 |
input_file = "" |
05 |
output_file = "" |
06 |
07 |
for op, in opts: |
08 |
if op = = "-i" : |
09 |
input_file = value |
10 |
elif op = = "-o" : |
11 |
output_file = value |
12 |
elif op = = "-h" : |
13 |
usage() |
14 |
sys.exit() |
代码解释:
a) sys.argv[1:]为要处理的参数列表,sys.argv[0]为脚本名,所以用sys.argv[1:]过滤掉脚本名。
b) "hi:o:": 当一个选项只是表示开关状态时,即后面不带附加参数时,在分析串中写入选项字符。当选项后面是带一个附加参数时,在分析串中写入选项字符同时后面加一个":"号。所以"hi:o:"就表示"h"是一个开关选项;"i:"和"o:"则表示后面应该带一个参数。
c) 调用getopt函数。函数返回两个列表:opts和args。opts为分析出的格式信息。args为不属于格式信息的剩余的命令行参数。opts是一个两元组的列表。每个元素为:(选项串,附加参数)。如果没有附加参数则为空串''。
getopt函数的第三个参数[, long_options]为可选的长选项参数,上面例子中的都为短选项(如-i -o)
长选项格式举例:
--version
--file=error.txt
让一个脚本同时支持短选项和长选项
getopt.getopt(sys.argv[1:], "hi:o:", ["version", "file="])
getopt函数的格式是getopt.getopt ( [命令行参数列表], "短选项", [长选项列表] )
短选项名后的冒号(:)表示该选项必须有附加的参数。
长选项名后的等号(=)表示该选项必须有附加的参数。
返回opts和args。
opts是一个参数选项及其value的元组( ( '-f', 'hello'), ( '-t', '' ), ( '--format', '' ), ( '--directory-prefix', '/home' ) )
args是一个除去有用参数外其他的命令行输入 ( 'a', 'b' )
然后遍历opts便可以获取所有的命令行选项及其对应参数了。
遍历opts可以获取所有命令的选项及参数,
一个简单的例子见下:
opts, args = getopt.getopt(sys.argv[1:], "hi:o:", ["help", "input=", "output="]) def usage(): print sys.argv[0] + ' -i inputfile -o outputfile' print sys.argv[0] + ' -h #get help info' input_file, output_file = '', '' for op, value in opts: if op == '-i' or op == '--input': input_file = value elif op == '-o' or op == '--output': output_file = value elif op == '-h': usage() sys.exit()
在命令行中运行的形式可以为以下几种:
$ python test.py -i ./input.txt -o ./output.txt
$ python test.py --input=./input.txt -o ./output.txt
$ python test.py -i ./input.txt --output=./output.txt
$ python test.py --input=./input.txt --output=./output.txt
$ python test.py -h