18. Python Command-line arguments

 



Command-line arguments

            The command-line argument is another way to read user-provided input. We have to pass arguments from the command prompt at execution time. And it’s accepted in the current file name only.

Ex:

py main.py 10 20 30

The above example (py or python) is the must at the starting. After (main.py) or your current working file name, after that  10,20,30 are arguments, in these arguments we can assign any type of data. And it’s always taken as a list type. Once we assign the data (sys module) will help collect the given data. In the sys module (argv) default variable is to collect the data from the command prompt and execute it as per our python program.

Ex:

from sys import argv

print(type(argv))

<class 'list'>                          #Result

IMP Point:

Argv takes as given file name is (0) th index. Check some examples on below.

from sys import argv

print(argv[0])

print(argv[:])

main.py                                              #Result

['main.py', '10', '20', '30']                 #Result

One more example:

from sys import argv

print(
'the number of command line arguments:',len(argv))
print(
'the number of command line arguments:', argv)
print(
'the cmd line arguments one by one', )
for x in argv:
   
print(x)

 

#Result

C:\Users\xxxx\PycharmProjects\Project001>py main.py 10 20 30

the number of command line arguments: 4

the number of command line arguments: ['main.py', '10', '20', '30']

the cmd line arguments one by one

main.py

10

20

30

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

 

#Result

C:\Users\xxxx\PycharmProjects\Project001>py main.py 10 20 30

the sum: 60

Some IMP Points:

Point Number 1:

Ex:

from sys import argv
print (argv[
:])
print (len(argv))

#Result

C:\Users\xxxx\PycharmProjects\Project001>py main.py 10 30 "gopi nath"

['main.py', '10', '30', 'gopi nath']

4

Here space is the object separator, if we assign some word like (gopi nath) with space then we must use the double quotes ("") only. Other types of the quote not allowed, because we assign arguments on the command prompt.

Point Number 2:

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

#Result

C:\Users\xxxx\PycharmProjects\Project001>py main.py 10 20 30

1020

30

Above example all command prompt arguments are taken as an ‘str’ type only. If we want to do the arithmetic operations then we need to convert through typecasting.

Point Number 3:

from sys import argv
print (argv[
100])

#Result

C:\Users\xxx\PycharmProjects\Project001>py main.py 10 20 30

Traceback (most recent call last):

  File "main.py", line 52, in <module>

    print (argv[100])

IndexError: list index out of range

In this case, we can apply within the range of the given indexing. If we assign more than that it will an error like index out of range error.


0 Comments

Post a Comment

Post a Comment (0)

Previous Post Next Post