FileNotFoundError,Python
FileNotFoundError, Python
我正在尝试打开文件夹中的文件,但出现以下错误:
FileNotFoundError: [Errno 2] No such file or directory: 'TRAIN_00000.eml'
我仔细检查了文件名和代码中写的directory/path,但问题仍然存在。
这是代码块:
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:
至:
for em in listing:
em = os.path.join(path, em) # this is what you need to add
这应该可以解决您的问题。 os.listdir()
中的 return 是相对路径列表。如果您不在 path 目录中调用应用程序,则需要将它们设为绝对路径。如您所见,否则找不到它们。
我正在尝试打开文件夹中的文件,但出现以下错误:
FileNotFoundError: [Errno 2] No such file or directory: 'TRAIN_00000.eml'
我仔细检查了文件名和代码中写的directory/path,但问题仍然存在。
这是代码块:
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:
至:
for em in listing:
em = os.path.join(path, em) # this is what you need to add
这应该可以解决您的问题。 os.listdir()
中的 return 是相对路径列表。如果您不在 path 目录中调用应用程序,则需要将它们设为绝对路径。如您所见,否则找不到它们。