2013-08-05 6 views
0

메신저 폴더에있는 많은 파일에서 반복 실행하려고 할 때 파일이 존재합니다. 이름을 볼 수있는 파일에서 파일을 인쇄하면 ... Im quite 프로그래밍에 익숙하지 않으면, 제게 손을 줘 주시겠습니까? 친절하게!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

나는 파이썬에 익숙하지 않지만 백 슬래시가 이상하게 보입니다. '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. 
관련 문제