在Python中使用命令行参数:了解sys.argv

Zac*_*iro 6 python argv command-line-arguments

我目前正在学习Python的艰难之路.我想这个例子可能已经过时了,所以我想在这里得到反馈.

我正在使用Python 3.1

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))
Run Code Online (Sandbox Code Playgroud)

我收到这个错误:

Traceback (most recent call last):
  File "/path/ch13.py", line 3, in <module>
    script, first, second, third, bacon = argv
ValueError: need more than 1 value to unpack
Run Code Online (Sandbox Code Playgroud)

知道什么是错的吗?

Ign*_*ams 9

你忘了将参数传递给脚本,例如foo.py bar baz quux.

在此输入图像描述