Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Command-Line Arguments

Python Command-Line Arguments

Python Basic Syntax Python Basic Syntax

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:

**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

print

'Number of arguments:'

,

len

(

sys

.

argv

)

,

' arguments.'

print

'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:

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

:

     

print

'test.py -i -o '

     

sys

.

exit

(

2

)

   

for

opt

,

arg

in

opts:

     

if

opt

==

'-h'

:

         

print

'test.py -i -o '

         

sys

.

exit

(

)

     

elif

opt

in

(

"-i"

,

"--ifile"

)

:

         inputfile

=

arg

     

elif

opt

in

(

"-o"

,

"--ofile"

)

:

         outputfile

=

arg

   

print

'The input file is:'

,

inputfile

   

print

'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 Python Basic Syntax