我尝试在python-3环境下学习这本书,但是当我尝试运行它时会弹出一个错误。有什么地方可以解决这个问题?
我删除
print("Ready, hit RETURN to continue, CTRL-C to abort.")
input()
代码运行完美。所以我认为这应该是python 2和python 3之间的语法问题
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 too, how?
in_file = open(from_file)
indata = in_file.read()
print("The input file is %d bytes long" % len(indata))
print("Does the output file exist? %r" % exists(to_file))
print("Ready, hit RETURN to continue, CTRL-C to abort.")
input()
out_file = open(to_file, 'w')
out_file.write(indata)
print("Alright, all done")
out_file.close()
in_file.close()
当我尝试运行它时,它应该停在input()上,等我按下返回键以继续。但实际上,代码停止了,并且出现了一个错误
"Traceback (most recent call last):
File "ex17.2.py", line 18, in <module> g
input("")
File "<string>", line 0
^
SyntaxError: unexpected EOF while parsing
弹出。
答案 0 :(得分:0)
是的,您是对的!您收到的错误是由于python版本更改。这段代码在python 3.x中工作得很好,但是在3.x下的python版本中失败。
希望这会有所帮助。