argument passing in python

in python when you execute your python files using interactive mode in python you could also pass the argument by adding it alongside with the filenames that you want to execute for example python tes.py 1 2 3 yes no you can get the list of argument above in the code by using lib built in form python called sys. import sys you can call all of the argument that you pass when you execute the files by using this import sys print(sys.argv) by excuting above code you will get all of the argument include the file name into the list. ['tes.py',1,2,3,'yes','no'] or you could access name file only by using code bellow, because by default files name always placed in the first list. print(sys.argv[0]) #it will return-->tes.py

Mar 21, 2025 - 20:16
 0
argument passing in python

in python when you execute your python files using interactive mode in python you could also pass the argument by adding it alongside with the filenames that you want to execute for example

python tes.py 1 2 3 yes no

you can get the list of argument above in the code by using lib built in form python called sys.

import sys

you can call all of the argument that you pass when you execute the files by using this

import sys

print(sys.argv)

by excuting above code you will get all of the argument include the file name into the list.

['tes.py',1,2,3,'yes','no']

or you could access name file only by using code bellow, because by default files name always placed in the first list.

print(sys.argv[0])
#it will return-->tes.py