herro,这里的菜鸟程序员正面临着两难选择,试图从终端运行一个简单的python脚本,它的运行方式完全一样
from sys import argv
script, food, echo, tree=argv
print("the script is called:", script)
print("the first variable is called:", food)
print("the second variable is called:", echo)
print("blah blah third is:", tree)
(脚本结尾)
在终端
python3.7 test.py 1 2 3
当我在空闲状态下按Enter键时,我只会收到“ valueError:没有足够的值要解压)
这让我发疯了,感谢大家的帮助,谢谢!
答案 0 :(得分:1)
它可以在Python 2-3.7中运行,但我必须进行一次更改。
更改
print("the script is called:", test.py)
到
print("the script is called:", script)
我想那可能只是一个错字。
答案 1 :(得分:1)
这是因为参数和列表解包不匹配 试试这个
from sys import argv
# argv always take default argument as file path
# at 0th position that is way 3+1
if len(argv) == 4:
script, food, echo, tree=argv
print("the script is called:", script)
print("the first variable is called:", food)
print("the second variable is called:", echo)
print("blah blah third is:", tree)
或像这样直接使用
print("the script is called:", argv[0])
答案 2 :(得分:0)
这有效:
from sys import argv
script, food, echo, tree=argv
print("the script is called:", script)
print("the first variable is called:", food)
print("the second variable is called:", echo)
print("blah blah third is:", tree)
$ python3 ~/tmp/e test.py 1 2
the script is called: /home/bhepple/tmp/e
the first variable is called: test.py
the second variable is called: 1
blah blah third is: 2