Python 没有这样的文件或目录错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4004299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 13:48:18  来源:igfitidea点击:

No such file or directory error

python

提问by Richard Knop

This is the error I am getting:

这是我得到的错误:

Traceback (most recent call last):
  File "E:\stuff\module.py", line 91, in <module>
    f = open('E:/stuff/log.txt')
IOError: [Errno 2] No such file or directory: 'E:/stuff/log.txt'

And this is my code:

这是我的代码:

f = open('E:/stuff/log.txt')

The E:/stuff/log.txtfile exists. I can navigate in Windows Explorer and open it so why can't I open it?

E:/stuff/log.txt文件存在。我可以在 Windows 资源管理器中导航并打开它,为什么我不能打开它?

EDIT:

编辑:

Output of DIR command:

DIR 命令的输出:

C:\Documents and Settings\Administrator>dir e:\stuff
 Volume in drive E has no label.
 Volume Serial Number is 5660-4957

 Directory of e:\stuff

23. 10. 2010  09:26    <DIR>          .
23. 10. 2010  09:26    <DIR>          ..
19. 10. 2010  20:07               385 index.py
23. 10. 2010  16:12             1?954 module.py
22. 10. 2010  19:16             8?335 backprop.py
19. 10. 2010  20:54             1?307 backprop-input.gif
19. 10. 2010  01:48               310 HelloWorld.kpf
23. 10. 2010  15:47                 0 log.txt.txt
               6 File(s)         12?291 bytes
               2 Dir(s)   8?795?586?560 bytes free



C:\Documents and Settings\Administrator>dir e:\
 Volume in drive E has no label.
 Volume Serial Number is 5660-4957

 Directory of e:\

16. 10. 2010  13:32    <DIR>          development-tools
23. 10. 2010  09:26    <DIR>          stuff
               0 File(s)              0 bytes
               2 Dir(s)   8?795?586?560 bytes free

I am running the python script from the cmd like this:

我正在从 cmd 运行 python 脚本,如下所示:

python E:\stuff\module.py

采纳答案by Tim ?as

Firstly, from above, Windows supports / just fine.

首先,从上面来看,Windows 支持 / 就好了。

Secondly: Well, if you look at your file, you'll notice it's not log.txt, it's log.txt.txt... You may see it as "log.txt" in your graphical folder viewer (as opposed to the CLI "dir" command) simply because it hides the known file extensions.

其次:好吧,如果您查看您的文件,您会注意到它不是 log.txt,而是 log.txt.txt...您可能会在图形文件夹查看器中将其视为“log.txt”(而不是CLI“dir”命令)只是因为它隐藏了已知的文件扩展名。

I recommend you disable this - see folder options, there should be an option "Hide extensions of known file types" (or similar).

我建议您禁用此功能 - 请参阅文件夹选项,应该有一个选项“隐藏已知文件类型的扩展名”(或类似选项)。

回答by tutuDajuju

how about reading permissions? maybe not authorized to read (default mode of open)

读取权限怎么样?可能未授权阅读(默认打开模式)

回答by Rawheiser

Since it is windows, and the backslash is a escape character, you must double the backslash to escape it. Try

由于是windows,而且反斜杠是转义字符,你必须把反斜杠加倍才能转义。尝试

e:\stuff\log.txt

回答by mouad

it been a long time that i didn't use windows, but if i remember well windows use back-slash in system path so you should do:

很长一段时间我没有使用 Windows,但如果我记得很好,Windows 在系统路径中使用反斜杠,所以你应该这样做:

import os

file_name = os.path.join("e:\stuff", "log.txt")

f = open(file_name)

and not:

并不是:

f = open('E:/stuff/log.txt')

there is not / in paths in windows.

Windows 中的路径中没有 / 。

回答by ghostdog74

Define you path names using os.path.join()

使用定义你的路径名 os.path.join()

root="E:\"
mylog = os.path.join(root,"stuff","log.txt") # or log.txt.txt as seen in your dir output
f = open(mylog)
...
f.close()

回答by brady

Look at this line in the "dir" output:

查看“dir”输出中的这一行:

23. 10. 2010  15:47                 0 log.txt.txt

The file you are looking for is named "log.txt.txt", not "log.txt". I see this happen when people set up the Windows file manager to not show known file extensions and then they try to add or modify an extension. I recommend to others that they turn this behavior off. You can do this under View->Folder Options I believe.

您要查找的文件名为“log.txt.txt”,而不是“log.txt”。我看到当人们将 Windows 文件管理器设置为不显示已知文件扩展名,然后他们尝试添加或修改扩展名时会发生这种情况。我建议其他人关闭这种行为。我相信您可以在“查看”->“文件夹选项”下执行此操作。