Python 错误:FileNotFoundError: [Errno 2] 没有那个文件或目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50011733/
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-19 19:21:48  来源:igfitidea点击:

Python error: FileNotFoundError: [Errno 2] No such file or directory

pythonpython-3.xfile

提问by Mayur Potdar

I am trying to open the file from folder and read it but it's not locating it. I am using Python3

我试图从文件夹中打开文件并读取它,但它没有找到它。我正在使用 Python3

Here is my code:

这是我的代码:

import os
import glob

prefix_path = "C:/Users/mpotd/Documents/GitHub/Python-Sample-                
codes/Mayur_Python_code/Question/wx_data/"
target_path = open('MissingPrcpData.txt', 'w')
file_array = [os.path.abspath(f) for f in os.listdir(prefix_path) if 
f.endswith('.txt')]
file_array.sort() # file is sorted list

for f_obj in range(len(file_array)):
     file = os.path.abspath(file_array[f_obj])
     join_file = os.path.join(prefix_path, file) #whole file path

for filename in file_array:
     log = open(filename, 'r')#<---- Error is here

Error: FileNotFoundError: [Errno 2] No such file or directory: 'USC00110072.txt'

Error: FileNotFoundError: [Errno 2] No such file or directory: 'USC00110072.txt'

回答by B?a?ej Michalik

You are not giving the full path to a file to the open(), just its name.

您没有将文件的完整路径提供给open(),而只是其名称。

You would have to either os.path.join()correct directory path to it, or os.chdir()to the directory that the files reside in.

您必须os.path.join()更正它的目录路径或os.chdir()文件所在的目录。

From your code I can deduce though, that you are forgetting to modify the the file_arraylist. To fix this, change the first loop to this:

不过,从您的代码中,我可以推断出您忘记修改file_array列表。要解决此问题,请将第一个循环更改为:

file_array = [os.path.join(prefix_path, name) for name in file_array]

Also, remember that os.path.abspath()can't deduce the full path to a file just by it's name.

另外,请记住,os.path.abspath()不能仅通过文件名推断出文件的完整路径。



Let me reiterate.

让我重申一下。

This line in your code:

您代码中的这一行:

file_array = [os.path.abspath(f) for f in os.listdir(prefix_path) if f.endswith('.txt')]

is wrong. It will not give you a list with correct absolute paths. What you should've done is:

是错的。它不会为您提供具有正确绝对路径的列表。你应该做的是:

import os
import glob

prefix_path = ("C:/Users/mpotd/Documents/GitHub/Python-Sample-"    
               "codes/Mayur_Python_code/Question/wx_data/")
target_path = open('MissingPrcpData.txt', 'w')
file_array = [f for f in os.listdir(prefix_path) if f.endswith('.txt')]
file_array.sort() # file is sorted list

file_array = [os.path.join(prefix_path, name) for name in file_array]

for filename in file_array:
     log = open(filename, 'r')

回答by jjj

You are using relative path where you should be using an absolute one. It's a good idea to use os.pathto work with file paths. Easy fix for your code is:

您正在使用相对路径,而您应该使用绝对路径。os.path用于处理文件路径是个好主意。您的代码的简单修复是:

prefix = os.path.abspath(prefix_path) 
file_list = [os.path.join(prefix, f) for f in os.listdir(prefix) if f.endswith('.txt')]

Note that there are some other issues with your code:

请注意,您的代码还有一些其他问题:

  1. In python you can do for thing in things. You did for thing in range(len(things))it's much less readable and unnecessary.

  2. You should use context managers when you open a file. Read more here.

  1. 在 python 中,你可以做for thing in things. 你这样for thing in range(len(things))做的可读性和不必要性要低得多。

  2. 打开文件时应该使用上下文管理器。在这里阅读更多。