IOError: [Errno 2] No such file or directory [duplicate]

两盒软妹~` 提交于 2019-12-13 07:07:05

问题


im having problems trying to run an iteration over many files in a folder, the files exist, if I print file from files I can see their names... Im quite new to programming, could you please give me a hand? kind regards!

import os
for path, dirs, files in os.walk('FDF\FDF'):
    for file in files:
        print file
        fdf = open(file, "r")
IOError: [Errno 2] No such file or directory: 'FDF_20110612_140613_...........txt'

回答1:


You need to prefix each file name with path before you open the file.

See the documentation for os.walk.

import os
for path, dirs, files in os.walk('FDF\FDF'):
    for file in files:
        print file
        filepath = os.path.join(path, file)
        print filepath
        fdf = open(filepath, "r")



回答2:


Try this:

import os

for path, dirs, files in os.walk('FDF\FDF'):
    for file in files:
        print file
        with open(os.path.join(path, file)) as fdf:
            # code goes here.


来源:https://stackoverflow.com/questions/18067799/ioerror-errno-2-no-such-file-or-directory

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!