Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何在Python3.x中传递命令行参数?_Python 3.x_Command Line Arguments - Fatal编程技术网

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中键入的参数完全相同

请注意,您提供的代码只有在由于元组解包而恰好传递了三个参数时才能正常工作

如果您要编写一个处理大量参数的程序,请参阅文档并查看文档