Python command-line arguments
Python command-line arguments
Python providesgetopt module to get command line arguments.
$ python test.py arg1 arg2 arg3
Python can also be used inthe sys sys.argvto get command line arguments:
sys.argv is a list of command line parameters.
len (sys.argv) is the number of command-line arguments.
NOTE: sys.argv [0] indicates the name of the script.
Examples
test.py file code is as follows:
#!/usr/bin/python # -*- coding: UTF-8 -*- import sys print '参数个数为:', len(sys.argv), '个参数。' print '参数列表:', str(sys.argv)
Implementation of the above code, the output is:
$ python test.py arg1 arg2 arg3 参数个数为: 4 个参数。 参数列表: ['test.py', 'arg1', 'arg2', 'arg3']
getopt module
getopt module is dedicated processing module command line parameters used to obtain command-line options and parameters, is sys.argv. Command-line option makes the program more flexible parameters. Support short option mode (-) and long options mode (-).
The module provides two methods and an exception handler to parse the command line arguments.
getopt.getopt method
getopt.getopt methods for parsing command-line parameter list syntax is as follows:
getopt.getopt(args, options[, long_options])
Method parameters:
args: To parse the command-line argument list.
options: to define the format string, the colon optionsafter (:) indicates that this option must have the additional parameters, without the colon indicates that this option is not additional parameters.
long_options: define the format of the list,long_options after the equal sign (=) said that if this option is set, there must be additional parameters, or not additional parameters.
This method returns a value consists of two elements: the first is the(option, value) tuples.The second is a list of parameters, including those that do not '-' or '-' parameter.
Another method is getopt.gnu_getopt, not much to do here described.
Exception getopt.GetoptError
Is not found in the list of parameters, or options desired parameter triggers the exception is empty.
Exception argument is a string that represents the error. Error message related options andopt for the msgattribute.
Examples
Suppose we create such a script, you can pass two lines to the script file filename command, while we use another option to view the script. Use the following script:
usage: test.py -i <inputfile> -o <outputfile>
test.py file code is as follows:
#!/usr/bin/python # -*- coding: UTF-8 -*- import sys, getopt def main(argv): inputfile = '' outputfile = '' try: opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="]) except getopt.GetoptError: print 'test.py -i <inputfile> -o <outputfile>' sys.exit(2) for opt, arg in opts: if opt == '-h': print 'test.py -i <inputfile> -o <outputfile>' sys.exit() elif opt in ("-i", "--ifile"): inputfile = arg elif opt in ("-o", "--ofile"): outputfile = arg print '输入的文件为:', inputfile print '输出的文件为:', outputfile if __name__ == "__main__": main(sys.argv[1:])
Implementation of the above code, the output is:
$ python test.py -h usage: test.py -i <inputfile> -o <outputfile> $ python test.py -i inputfile -o outputfile 输入的文件为: inputfile 输出的文件为: outputfile