Command Line Arguments ( sys.argv )in Python



Command Line Arguments ( sys.argv )in Python



sys.argv - The list of command line arguments passed to a Python script.

shebang lineThe first line of all your Python programs should be a shebang line, which tells your computer that you want Python to execute this program. The shebang line begins with #!


#understand with simple program Program-----


import sys
print(sys.argv)


#save as new.py    ( i have saved to Desktop as new.py)
#First run this on python shell. Then use cmd(command prompt )


You can see output of my current working Directory, where i have saved this file.

On command prompt



you can see here  new.py  is 1st argument (ie-[0] - index) , when i  passed LoL then it became 2nd argument(ie-[1]- index) and so on.....

Output is in the form of List.


#Another example----

import sys

if sys.argv[1] == 'vowels':
    print('A, E, I, O, U')
elif sys.argv[1] == 'consonant':
    print('B, C, D, F, G, H, J, K, L, M, N, P, Q, R, S, T, V, X, Z')




#see output


Here new.py is first argument on [0] -index and vowels is second argument on [1]- index,similarly  consonant is second argument on [1]- index.






Comments