Python provides us with different ways to handle inputs while programming. One such way is to use command line arguments for inputs while executing a Python file. The command line arguments in a Python program are stored in the sys.argv list. In this article, we will discuss the sys.argv list in Python and its uses with an example.
What is sys.argv in Python?
The sys.argv variable represents the argument vectors of for a program. The sys.argv list is a list that contains command line arguments in a Python program. Here, sys represents the system and argv stands for argument vector. Whenever we run a Python program from a command line interface, we can pass different arguments to the program. The program stores all the arguments and the file name of the Python file in the sys.argv list.
- The first element of the sys argv list contains the name of the Python file.
- The second element onwards contains the command line arguments.
You can also find the total number of command line arguments to a program. For this, you can find the length of the sys.argv list using the len() function. The len() function takes the list as its input and returns the length of the sys argv list. The total number of arguments in a Python program is one less than the length of the sys.argv list.
If you are going to work with command line arguments with a Python program, you probably want to use the sys argv list in your program.
Sys.Argv List Example in Python
To use sys argv, you will first have to import the sys
module. Then, you can obtain the name of the Python file and the value of the command line arguments using the sys argv list.
- The sys.argv list contains the name of the Python file at index 0.
- It contains the first command line argument at index 1.
- The second command line argument is present at index 2 and so on.
You can observe this in the following example.
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)
Output:
In the above example, you can observe that we have created a program sysargv.py and executed it using the command prompt. You can observe that the sys.argv list contains only one element i.e. the name of the program file.
Now, let us change the program and add four command line arguments to the Python file while executing it.
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[0]
first_argument=sys.argv[1]
second_argument=sys.argv[2]
third_argument=sys.argv[3]
fourth_argument=sys.argv[4]
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)
Output:
In the above example, you can observe that we have created a program sysargv.py. While executing it through the command prompt, we passed 4 values as input arguments to the file.
You can observe that the name of the Python file is still at the first position in the list. The next 4 elements of the sys.argv list are the input arguments that we passed.
To learn more about sys.argv list, you can read this article on command line arguments in python.
Conclusion
In this article, we have discussed the sys argv list and its use in Python. To learn more about Python programming, you can read this article on open file using with open 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!
Happy Learning!
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.