Command Line Arguments

When we are executing the python program in the command line, we have to pass python filename.py. Here along with the file name, we can pass different values to the program, so these values are called command-line arguments.

>python filename.py value1 value2 ..
>python add.py 10 20
  • add.py is the file name (command line argument)
  • 10 is the value(command line argument)
  • 20 is the value(command line argument)

By default, python can have multiple command-line arguments that can be accessed through sys module, where sys is a default module that contains a variable called argv by using this we can access various types of command-line arguments.

The argv is List datatype, to understand this, let us consider below example

from sys import argv
print(type(argv))

cmnd-line-argv-one

In the case of command-line arguments in python, the first argument is always the python file name,

from sys import argv
print(type(argv))
print(argv[0])

I am going to save this file in a test1.py and execute; this program will print, which is the first argument(the first argument always has an index of zero). I execute the file

python test1.py 10 20 30

cmnd-line-argv-two

cmnd-line-argv-three

cmnd-line-argv-four

cmnd-line-argv-five

If you print(argv), then python will print all the list of arguments cmnd-line-argv-six

Next, consider another one example to print the number of arguments and the list of command-line arguments and also to print the command line arguments by one by.

from sys import argv
print("The number of command line arguments:", len(argv))
print("The list of command line arguments:",argv)
print("The command line argument one by one:")
for x in argv:
print(x)

cmnd-line-argv-seven

As you know before, by default python is going to consider file name as its first argument, and if we do not want to print the filename as the argument, by using the slicing operator, we can print only the explicitly given values as the arguments.

The following example to demonstrate By using the Slice operator to print the given values as the arguments.

#Program to print sum of given numbers provided as command line arguments:
from sys import argv
x=argv[1: ]
sum=0
for y in args:
    sum=sum+y:
print('The sum:", sum)

If we try to execute the above code, then we are going to get an error because whatever values we are going to pass through the command-line argument is always considered as a String data type, so in case of sum= sum+y, x is considered as a string, and the sum is considered as a number, so whenever we try to add a string with an integer, immediately, we are going to get a syntax error.

cmnd-line-argv-eight

E:Pythonpython codes>python test3.py
  File "test3.py", line 5
    sum=sum+y:
             ^
SyntaxError: invalid syntax

So, let us see how to correct it, instead of taking the only y, perform typecasting to convert the string into int type.

from sys import argv
x=argv[1: ]
sum=0
for y in x:
    sum=sum+int(y)
print('The sum:',sum)

cmnd-line-fifteen

If we execute the above program without passing any arguments, then the sum will be 0.

cmnd-line-argv-nine

Features of Command-Line Arguments

Space is the separator between the command line arguments if the argument itself contains the space, then it should be enclosed within the double-quotes.

from sys import argv
print(argv[1])​

Here, I am going to execute this code through command prompt and passing the argument as king kong.

command-line-argv-ten

If you observe, python is considering the king as the first argument and prints the same, and if we want to print king kong as one argument only then we have to enclose this with double-quotes.

from sys import argv
print(argv[1])
​

cmnd-line-argv-eleven

And if we use single quotes, then the output will like below

cmnd-line-argv-twelve

Inside the python program command-line arguments available only in the form of the string, if we try to add two string, then it will result in concatenation operation and hence to overcome this problem we have to convert these string datatype into an int by typecasting

from sys import argv
print(int(argv[1])+int(argv[2]))

cmnd-line-argv-thirteenpng

If we try to access the command line argument with out of range argument, then Python will throw an Index error.

from sys import argv
print(argv[100])
​

cmnd-line-argv-fourteen

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions