0

herro,noob 程序员在这里面临着相当的困境,试图从终端运行一个简单的 python 脚本,它完全一样

文件名=test.py

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

当我在空闲时按回车时,我只是不断收到“valueError:没有足够的值来解压)

这让我彻底疯了,感谢您的帮助,谢谢!

4

3 回答 3

1

好吧,它在 Python 2-3.7 中运行,但我不得不做一个改变。

更改
print("the script is called:", test.py)

print("the script is called:", script)

我想这可能只是一个错字。

于 2019-01-04T07:10:24.400 回答
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])
于 2019-01-04T07:14:56.703 回答
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
于 2019-01-04T07:19:28.437 回答