Command Line Arguments in Python: sys.argv, argparse
In Python, you can use sys.argv
or the argparse
module to handle command line arguments. The sys
and argparse
modules are both included in the standard library, so no additional installation is required.
- sys.argv — System-specific parameters and functions — Python 3.11.4 documentation
- argparse — Parser for command-line options, arguments and sub-commands — Python 3.11.4 documentation
To take input from the keyboard in your program, use the input()
function.
For information on handling arguments in functions instead of command line arguments, refer to the following article.
sys.argv
vs the argparse
module
sys.argv
is very simple and easy to use; however, you need to manage the number of arguments and perform type conversion yourself. On the other hand, while using the argparse
module requires some configuration code, it allows you to utilize options and manage any number of arguments.
If the number of arguments is fixed and simple, sys.argv
is usually sufficient. However, for programs used as command line tools, argparse is more convenient due to its flexible argument handling capabilities.
How to use sys.argv
Here is an example script:
import sys
print('sys.argv : ', sys.argv)
print('type(sys.argv) : ', type(sys.argv))
print('len(sys.argv) : ', len(sys.argv))
print()
print('sys.argv[0] : ', sys.argv[0])
print('sys.argv[1] : ', sys.argv[1])
print('sys.argv[2] : ', sys.argv[2])
print('type(sys.argv[0]): ', type(sys.argv[0]))
print('type(sys.argv[1]): ', type(sys.argv[1]))
print('type(sys.argv[2]): ', type(sys.argv[2]))
The results of running the python
(or python3
) command with arguments are as follows. Arguments are specified after the script file path, separated by spaces.
$ python3 sys_argv_test.py a 100
sys.argv : ['sys_argv_test.py', 'a', '100']
type(sys.argv) : <class 'list'>
len(sys.argv) : 3
sys.argv[0] : sys_argv_test.py
sys.argv[1] : a
sys.argv[2] : 100
type(sys.argv[0]): <class 'str'>
type(sys.argv[1]): <class 'str'>
type(sys.argv[2]): <class 'str'>
As you can see from this result, sys.argv
handles command line arguments as follows:
sys.argv
is a list- You can get the number of items (= the number of arguments) with
len()
- You can get the number of items (= the number of arguments) with
- The first element stores the script file path
- Whether this is a full path or not depends on the operating system
- All items are strings (
str
)- Even if specified as a number, it is stored as a string
- Use
int()
orfloat()
to convert to a number
See the following articles for len()
to get the number of items in a list, and int()
and float()
to convert strings to numbers.
In the sample code above, specifying fewer arguments than expected causes an error.
$ python3 sys_argv_test.py a
sys.argv : ['sys_argv_test.py', 'a']
type(sys.argv) : <class 'list'>
len(sys.argv) : 2
sys.argv[0] : sys_argv_test.py
sys.argv[1] : a
Traceback (most recent call last):
File "sys_argv_test.py", line 11, in <module>
print('sys.argv[2] : ', sys.argv[2])
IndexError: list index out of range
To deal with such cases, for example, you can either use len()
to get the number of arguments and then use an if
statement for conditional branching, or handle exceptions.
The argparse
module
For more flexible command handling, the argparse
module allows you to define argument types, accept multiple arguments as a list, and use options like -i
and -o
.
For a detailed explanation of the argparse
module's usage, refer to the comprehensive official tutorial. While argparse is versatile with extensive features, the quickest way to learn is by going through the tutorial from start to finish.