The sys module in Python provides us with many tools to interact with the system using Python programs. In this article, we will discuss how we can use command line arguments using sys.argv list in Python.
What is sys.argv List in Python?
In a Python program, sys.argv is the list of command line arguments passed to the program while executing it through the command line interface. Here,
- sys is the Python sys module.
- argv represents all the arguments that come along via the command line input.
Thus, sys.argv is basically an array holding the command line arguments of our program.
The first element in the sys.argv list is the name of the program itself. The arguments passed to the program are present at index 1 onwards in the sys.argv list in the same that are passed while executing the program. Don’t forget that the counting starts at zero (0), not one (1).
How to Use Command Line Arguments in Python?
To use command line arguments in Python, you will first have to import the sys module. After this, you can access all the command line arguments using the sys.argv list. To understand how the command line arguments, let us write a program and execute it using command line interface.
import sys print("Hi, I am PFB") print("The sys.argv list is:",sys.argv) sys_argv_length=len(sys.argv) number_of_arguments=sys_argv_length-1 print("Total command line arguments are:",number_of_arguments)
In the above example, we first print the sys.argv list using the
print() function. Then, calculated the length of the sys.argv list using the
len() function. You can observe that the only element in the sys.argv list is
argvsys.py that is the name of the program file.
Now, let us give some command line arguments as input to the program.
import sys print("Hi, I am PFB") print("The sys.argv list is:",sys.argv) sys_argv_length=len(sys.argv) number_of_arguments=sys_argv_length-1 print("Total command line arguments are:",number_of_arguments) file_name=sys.argv first_argument=sys.argv second_argument=sys.argv third_argument=sys.argv fourth_argument=sys.argv print("The filename is:",file_name) print("The first command line argument is:",first_argument) print("The second command line argument is:",second_argument) print("The third command line argument is:",third_argument) print("The fourth command line argument is:",fourth_argument)
In this example, we passed four command line arguments to the program. You can observe that the first argument,
sys.argv, is the name of the program as it was invoked,
sys.argv is the first argument you pass to the program. The next elements in the list are also consecutive command line arguments.
To access only the list of arguments and avoid the file name, you can slice the sya.argv list to access the actual command line arguments using the following syntax.
arguments = sys.argv[1:]
The above statement performs list slicing to select elements from index 1 to the last element of the sys.argv list. After obtaining the list of command line arguments, you can use them in your program however you want.
In this article, we discussed how command line arguments are stored in a python program. We also discussed how to use the command line arguments in our code by slicing the sys.argv list. To learn more about python programming, you can read this article on how to split a text file in Python. You might also like this article on string manipulation in Python.
I hope you enjoyed reading this article. Stay tuned for more informative articles.
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.