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 nopython tes.py 1 2 3 yes nopython tes.py 1 2 3 yes no
Enter fullscreen mode Exit fullscreen mode
you can get the list of argument above in the code by using lib built in form python called sys.
import sysimport sysimport sys
Enter fullscreen mode Exit fullscreen mode
you can call all of the argument that you pass when you execute the files by using this
import sysprint(sys.argv)import sys print(sys.argv)import sys print(sys.argv)
Enter fullscreen mode Exit fullscreen mode
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']['tes.py',1,2,3,'yes','no']['tes.py',1,2,3,'yes','no']
Enter fullscreen mode Exit fullscreen mode
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.pyprint(sys.argv[0]) #it will return-->tes.pyprint(sys.argv[0]) #it will return-->tes.py
Enter fullscreen mode Exit fullscreen mode
暂无评论内容