-2

我目前正在学习 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.

为什么是这样?提前致谢。

4

1 回答 1

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 命令。

展望未来,您可以做以下三件事:

于 2019-06-08T09:19:37.487 回答