Python 中的 Agrv 需要多个值才能解包?
Agrv in Python Needs More Than One Value to Unpack?
我一直在关注 Zed A. Shaw 的 "Learn Python the Hard Way" 这本书。我目前在练习 18,从练习 16 开始就遇到这个问题。我的代码是这样的:
from sys import argv
from os.path import exists
script, from_file, to_file = argv
print "Copying from %s to %s" % (from_file, to_file)
# we could do these two on one line, how?
in_file = open(from_file)
indata = in_file.read()
print "The input file is %d bytes long %r" % len(indata)
print "Ready, hit RETURN to continues, CTRL-C to abort."
raw_input()
out_file = open(to_file, 'w')
out_file.write(indata)
print "Alright, all done."
out_file.close()
in_file.close()
终端 (macOS) 中的结果是:
Traceback (most recent call last):
File "ex17.py", line 4, in <module>
script, from_file, to_file = argv
ValueError: need more than 2 values to unpack
我正在使用 macOS High Serria,使用 Python 2.7(非内置)在 Atom 中编码。
谢谢!
script, from_file, to_file = argv
这一行意味着它应该接受来自 运行 命令的三个值:
python test.py FileA FileB
您的脚本 test.py
from_file 是 FileA
并且 to_file 是 FileB
我一直在关注 Zed A. Shaw 的 "Learn Python the Hard Way" 这本书。我目前在练习 18,从练习 16 开始就遇到这个问题。我的代码是这样的:
from sys import argv
from os.path import exists
script, from_file, to_file = argv
print "Copying from %s to %s" % (from_file, to_file)
# we could do these two on one line, how?
in_file = open(from_file)
indata = in_file.read()
print "The input file is %d bytes long %r" % len(indata)
print "Ready, hit RETURN to continues, CTRL-C to abort."
raw_input()
out_file = open(to_file, 'w')
out_file.write(indata)
print "Alright, all done."
out_file.close()
in_file.close()
终端 (macOS) 中的结果是:
Traceback (most recent call last):
File "ex17.py", line 4, in <module>
script, from_file, to_file = argv
ValueError: need more than 2 values to unpack
我正在使用 macOS High Serria,使用 Python 2.7(非内置)在 Atom 中编码。
谢谢!
script, from_file, to_file = argv
这一行意味着它应该接受来自 运行 命令的三个值:
python test.py FileA FileB
您的脚本 test.py
from_file 是 FileA
并且 to_file 是 FileB