Python Command-Line Arguments
Python provides the getopt module to get command-line arguments.
$ python test.py arg1 arg2 arg3
Python can also use sys 's sys.argv to get command-line arguments:
-
sys.argv is the list of command-line arguments.
-
len(sys.argv) is the number of command-line arguments.
**Note:** sys.argv[0] is the script name.
Example
The test.py file code is as follows:
Example
#!/usr/bin/python
-- coding: UTF-8 --
import
sys
'Number of arguments:'
,
len
(
sys
.
argv
)
,
' arguments.'
'Argument list:'
,
str
(
sys
.
argv
)
The output of the code above is:
$ python test.py arg1 arg2 arg3
参数个数为: 4 个参数。
参数列表: ['test.py', 'arg1', 'arg2', 'arg3']
The getopt module
The getopt module is a module specifically for handling command-line arguments, used to obtain command-line options and parameters, that is, sys.argv. Command-line options make program arguments more flexible. It supports short option mode - and long option mode --.
This module provides two methods and one exception for parsing command-line arguments.
The getopt.getopt method
The getopt.getopt method parses a list of command-line arguments with the following syntax:
getopt.getopt(args, options[, long_options])
Method parameter description:
-
args: the list of command-line arguments to parse.
-
options : defined as a string; theoptions following colon : means that if the option is set, it must have an additional argument; otherwise it takes no argument.
-
long_options : defined as a list; thelong_options following equals sign = indicates that the option must have an additional argument; without an equals sign, the option takes no argument.
-
The method returns two elements: the first is a (option, value) a list of tuples. The second is a list of arguments containing those that do not - or -- 's parameter.
Another method is getopt.gnu_getopt, which will not be introduced in detail here.
Exception getopt.GetoptError
This exception is raised when the argument list is not found, or when a required argument for an option is empty.
The exception's argument is a string describing the cause of the error. The attribute msg and opt holds the error message for the related option.
Example
Suppose we create a script that receives two file names from the command line, along with another option for viewing the script's usage. The script is used as follows:
usage: test.py -i -o
The test.py file code is as follows:
Example
#!/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
:
'test.py -i
sys
.
exit
(
2
)
for
opt
,
arg
in
opts:
if
opt
==
'-h'
:
'test.py -i
sys
.
exit
(
)
elif
opt
in
(
"-i"
,
"--ifile"
)
:
inputfile
=
arg
elif
opt
in
(
"-o"
,
"--ofile"
)
:
outputfile
=
arg
'The input file is:'
,
inputfile
'The output file is:'
,
outputfile
if
name
==
"main"
:
main
(
sys
.
argv
[
1
:
]
)
The output of the code above is:
$ python test.py -h
usage: test.py -i -o
$ python test.py -i inputfile -o outputfile
输入的文件为: inputfile
输出的文件为: outputfile
Python Basic Syntax