Python 3.x 如何在Python3.x中传递命令行参数?
我没有得到这个程序想要的输出Python 3.x 如何在Python3.x中传递命令行参数?,python-3.x,command-line-arguments,Python 3.x,Command Line Arguments,我没有得到这个程序想要的输出 from sys import argv script, first, second, third = argv print ("The script is called:", script) print ("Your first variable is:", first) print ("Your second variable is:", second) print ("Your third variable is:", third) 如何使用cmd传递这
from sys import argv
script, first, second, third = argv
print ("The script is called:", script)
print ("Your first variable is:", first)
print ("Your second variable is:", second)
print ("Your third variable is:", third)
如何使用cmd传递这些参数?您可以这样称呼它
python program.py a1 b2 c3
它输出
The script is called: /home/sophia/program.py
Your first variable is: a1
Your second variable is: b2
Your third variable is: c3
sys.argv
包含字符串列表,每个字符串对应一个命令行参数。第一个始终是脚本的文件名;其他参数是可选参数,顺序与在shell中键入的参数完全相同
请注意,您提供的代码只有在由于元组解包而恰好传递了三个参数时才能正常工作
如果您要编写一个处理大量参数的程序,请参阅文档并查看文档