script,first,second,third=argv运行出错的问题

看“笨办法”学python,的问题

 1 #!/usr/bin/python36
 2 
 3 from sys import argv
 4 
 5 script, first, second, third = argv
 6 
 7 print("The script is called:", script)
 8 print("Your first variable is:", first)
 9 print("Your second variable is:",second)
10 print("Your third variable is:", third)

不管是在那里直接运行都提示错误,原因如下:

sys.argv是传递给python脚本的命令行参数【字符串】列表
argv[0]为该脚本自身路径,其余为命令行参数

你交互式运行没有参数“script,first,second,thrid=argv”这句自然会报错
请在命令行下(CMD)输入传递其他三个参数:
python 文件名.py 参数1 参数2 参数3

 

posted on 2018-02-06 16:40  新手爱好者  阅读(531)  评论(0编辑  收藏  举报

导航