Python命令列輸入參數 #Example 3
Example code 1 :
import sysdef main():
#intValue = int(sys.argv[1])#如果要將變數搞成數字的話可以使用 int()來轉
print (sys.argv[1])
print (sys.argv[2])
print (sys.argv[3])
print ("===============")
print (len(sys.argv)) #參數一共有幾個
print ("===============")
for x in sys.argv:
print (x)
if __name__ == "__main__":
main()
執行結果
Value1
Program files(x86)
123
===============
4
===============
.\pySystem2.py
Value1
Program files(x86)
123
Example cdoe 2 :
import getoptimport sys
opts,args = getopt.getopt(sys.argv[1:],'-h-f:-v',['help','filename=','version'])
print(opts)
for opt_name,opt_value in opts:
if opt_name in ('-h','--help'):
print("[*] Help info")
sys.exit()
if opt_name in ('-v','--version'):
print("[*] Version is 0.01 ")
sys.exit()
if opt_name in ('-f','--filename'):#当外部输入'-f1'或者'--flilename=1'时,输出为:[('--filename', '3')] \n [*] Filename is 3
fileName = opt_value
print("[*] Filename is ",fileName)
# do something
sys.exit()
留言
張貼留言