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

6

我目前正在学习《笨办法学 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))

我遇到了这个错误:

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

有什么问题吗?
11个回答

0

我认为这个例子会对你有所帮助。你可以传递任意数量的参数,也就是说,参数的数量是可变的。:D

def main(*args):
    print("Arguments Passed: ", args)

if __name__ == '__main__':
    name_Script, *script_args = sys.argv
    print("Name of the script: ", name_Script)
    main(*script_args) #Send list of arguments

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接