Command Line Argument Using sys.argv in Python

In python, we sometimes need to accept command-line arguments in our program. In this article, we will discuss how we can take user input in the command-line argument and access it using sys.argv list in Python.

What is sys.argv List in Python?

The meaning of sys.argv in Python is as follows.

  • sys stands for the sys module. The sys module is used to access and manipulate system data and resources in Python.
  • argv stands for argument vector. It is a python list containing the inputs passed in the command line argument. 

Therefore, sys.argv is a list in Python that contains the values given as input in the command line argument while executing the program.

You can print the sys.argv list using the print statement as shown in the following example.

import sys
print("Hi, I am avidpython.")
print("The sys.argv list is:",sys.argv)

We execute the above program in the command line as shown below.

sys.argv example in Python
sys.argv example 1

In the output above, you can see that the python program’s name is an element in the sys.argv list. We haven’t passed any other command line input. Therefore, there is only one element in the sys.argv list.

You can observe that the name of the program is the first element of the list. In python, the program’s name is always the first element of the sys.argv list. You can access the name of the program using list indexing as shown below.

import sys
print("Hi, I am avidpython.")
print("The sys.argv list is:",sys.argv)
program_name=sys.argv[0]
print("The name of program is:",program_name)

Output:

sys.argv example in Python
sys argv example 2

In the above example, we have used list indexing to obtain the name of the program from the sys.argv list.

Command Line Argument Using sys.argv List in Python

As said above, we can also use the sys.argv list to take command-line arguments in Python. The arguments are given in the command line terminal separated by a space character.

The syntax for passing command line argument to a python program is as follows.

python3 program_name.py argument1 argument2 argument3

Here, each command-line argument becomes the element of the sys.argv list in the same order it is written in the command line terminal while executing the program as shown in the following example.

import sys
print("Hi, I am avidpython.")
print("The sys.argv list is:",sys.argv)
program_name=sys.argv[0]
print("The name of program is:",program_name)

Output:

sys.argv example in Python
sys argv example 3

In the above example, you can observe that we have passed “Avidpython“, 1, 2, and 1117 as the command line arguments to the program. The values have become the element of sys.argv list in the same order.

Example Programs Using sys.argv List in Python

You can find the total number of command-line arguments using the sys.argv list in Python. We will pass the sys.argv list to the len() function as an input argument. The len() function will return the length of the sys.argv list. 

As the first element of the sys.argv list is the name of the program, you can find the number of command-line arguments by subtracting 1 from the length as shown below.

import sys
print("Hi, I am avidpython.")
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:

sys.argv example in Python
sys argv example 4

You can also access and print all the command line arguments using the sys.argv list as shown in the following example.

import sys
print("Hi, I am avidpython.")
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)
print("The first command line argument is:",sys.argv[1])
print("The second command line argument is:",sys.argv[2])
print("The third command line argument is:",sys.argv[3])
print("The fourth command line argument is:",sys.argv[4])

Output:

sys.argv example
sys argv example 5

In the above example, we have directly accessed and printed the command line arguments. However, you can also store the command line arguments in specific variables as shown below.

import sys
print("Hi, I am avidpython.")
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)
first_argument=sys.argv[1]
second_argument=sys.argv[2]
third_argument=sys.argv[3]
fourth_argument=sys.argv[4]
print("The first command line argument is:",first_argument)
print("The first command line argument is:",second_argument)
print("The first command line argument is:",third_argument)
print("The first command line argument is:",fourth_argument)

Output:

sys.argv example

In this example, we have first stored the command line arguments in a python variable. After that, we have printed the values using the print function in Python.

Conclusion

In this article, we have discussed the working of sys.argv in Python. To know more about python programming, you can read this article on when to use try-except instead of if-else in Python. You can also also like this article on list comprehension in Python.

I hope you enjoyed reading this article. Stay tuned for more informative articles.

Happy Learning!

Donate to Avid Python

Dear reader,
If you found this article helpful and informative, I would greatly appreciate your support in keeping this blog running by making a donation. Your contributions help us continue creating valuable content for you and others who come across my blog. 
No matter how big or small, every donation is a way of saying "thank you" and shows that you value the time and effort we put into writing these articles. If you feel that our article has provided value to you, We would be grateful for any amount you choose to donate. 
Thank you for your support! 
Best regards, 
Aditya
Founder

If you want to Pay Using UPI, you can also scan the following QR code.

Payment QR Code
Payment QR Code

Leave a Reply