我试图打开文件夹中的文件,但结果却出现了以下错误:
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)任何帮助都是非常感谢的。:)
发布于 2017-12-10 03:16:18
更改:
for em in listing:至:
for em in listing:
em = os.path.join(path, em) # this is what you need to add这应该能解决你的问题。从os.listdir()返回的是一个相对路径列表。如果没有调用路径目录中的应用程序,则需要使它们成为绝对路径。否则就找不到了,就像你看到的。
https://stackoverflow.com/questions/47735473
复制相似问题