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
python tes.py 1 2 3 yes no
python 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 sys
import sys
import 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 sys
print(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.py
print(sys.argv[0])
#it will return-->tes.py
print(sys.argv[0]) #it will return-->tes.py

Enter fullscreen mode Exit fullscreen mode

原文链接:argument passing in python

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享
I'm not lazy, I'm on energy saving mode.
我不懒,我只是开启了节能模式
评论 抢沙发

请登录后发表评论

    暂无评论内容