我目前正在学习Zed Shaw的LPTHW,但发现了问题。
我正在尝试自己编写整个代码(当然没有行号)
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."
raw_input()
out_file = open(to_file, 'w')
out_file.write(indata)
print "Alright, all done."
out_file.close()
in_file.close()
,但是代码不起作用。我一直在寻找错误,但似乎找不到任何错误,因此出于无奈,我删除了编写的代码并重写了新代码,但是这次我完全从电子书中复制了代码以确保我没有犯任何错误。事实证明还不错。 cmd改为显示
D:\Coding>python ex17.py test.txt new_file.txt
Copying from test.txt to new_file.txt
Traceback (most recent call last):
File "ex17.py", line 9, in <module>
in_file = open(from_file)
IOError: [Errno 2] No such file or directory: 'test.txt'
我真的不知道问题出在哪里,因为直接从电子书中复制代码也无济于事。另外,当我尝试插入cat命令时收到了此消息。
D:\Coding>cat test.txt
'cat' is not recognized as an internal or external command,
operable program or batch file.
这是为什么?预先感谢。
答案 0 :(得分:0)
D:\Coding>python ex17.py test.txt new_file.txt
Copying from test.txt to new_file.txt
Traceback (most recent call last):
File "ex17.py", line 9, in <module>
in_file = open(from_file)
IOError: [Errno 2] No such file or directory: 'test.txt'
最后一个回溯告诉您文件text.txt
不存在。
当您的程序尝试读取它时,它就会崩溃。
D:\Coding>cat test.txt
'cat' is not recognized as an internal or external command,
operable program or batch file.
如评论Vikas Gautam所述,Windows无法理解cat
,因为它是Linux命令。
前进,这是您可以做的三件事: