FileNotFoundError,Python

FileNotFoundError,Python,python,python-3.x,path,file-not-found,Python,Python 3.x,Path,File Not Found,我试图打开文件夹中的文件,但出现以下错误: FileNotFoundError: [Errno 2] No such file or directory: 'TRAIN_00000.eml' 我仔细检查了代码中的文件名和目录/路径,但问题仍然存在 下面是代码块: import os path = "C:\\Users\\...\\TRAINING" listing = os.listdir(path) for em in listing: file = open(em, 'rb'

我试图打开文件夹中的文件,但出现以下错误:

FileNotFoundError: [Errno 2] No such file or directory: 'TRAIN_00000.eml'
我仔细检查了代码中的文件名和目录/路径,但问题仍然存在

下面是代码块:

import os

path = "C:\\Users\\...\\TRAINING"
listing = os.listdir(path)


for em in listing:
    file = open(em, 'rb')
    e_content = file.read()
    file.close()

print (e_content)
感谢您的帮助。:)

变化:

for em in listing:
致:

这应该能解决你的问题。从
os.listdir()
返回的是一个相对路径列表。如果您没有在path目录中调用应用程序,则需要将它们设置为绝对路径。否则,将无法找到它们,如您所见。

更改:

for em in listing:
致:

这应该能解决你的问题。从
os.listdir()
返回的是一个相对路径列表。如果您没有在path目录中调用应用程序,则需要将它们设置为绝对路径。如您所见,否则将找不到它们。

仅返回文件名,您必须
os.path.join()
使用路径连接它们。
os.listdir()
仅返回文件名,您必须
os.path.join()
使用路径连接它们。