0

我在尝试对文件夹中的许多文件进行迭代时遇到问题,这些文件存在,如果我从文件中打印文件,我可以看到它们的名称......我对编程很陌生,你能帮帮我吗?亲切的问候!

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'
4

2 回答 2

0

path在打开文件之前,您需要为每个文件名添加前缀。

请参阅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")
于 2013-08-05T21:39:49.333 回答
0

尝试这个:

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.
于 2013-08-05T21:40:15.303 回答