2013-08-05 46 views
0

即时尝试运行一个文件夹中的许多文件迭代,文件存在,如果我从文件打印文件,我可以看到他们的名字... Im very新编程,你能帮我一下吗?亲切的问候!IOError:[Errno 2]没有这样的文件或目录

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' 
+0

http://stackoverflow.com/a/9765314/1350424或 http://docs.python.org/2/tutorial/inputoutput.html#methods -of-file-objects – eclipsis

+0

你应该使用你使用的技术(在这种情况下是python)添加标签,这样你的问题就会出现在相关的地方。我为你添加了它。 – mnagel

+0

我对python没有经验,但对我来说''FDF \ FDF''这个反斜杠似乎很奇怪。路径通常使用'/'。 – mnagel

回答

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") 
+0

谢谢大家,问题解决了 – Patowski

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. 
相关问题