루프를 통해 원하는 폴더가 10 개 있습니다. 내가 파일의 이름을 인쇄 할 때 내 코드는 잘 작동 :파이썬으로 파일을 반복하는 방법은 무엇입니까?
1
2
3
4
5
6
7
8
9
10
을하지만 루프에서 파일을 열려고하면 내가 IO 오류가 :
import os
indir = '/home/des/test'
for root, dirs, filenames in os.walk(indir):
for f in filenames:
print(f)
인쇄 어느
import os
indir = '/home/des/test'
for root, dirs, filenames in os.walk(indir):
for f in filenames:
log = open(f, 'r')
Traceback (most recent call last):
File "/home/des/my_python_progs/loop_over_dir.py", line 6, in <module>
log = open(f, 'r')
IOError: [Errno 2] No such file or directory: '1'
>>>
루프 내부에서도 파일의 전체 경로를 전달해야합니까?
당신이 디렉토리 트리를 탐색하려고를, 또는 특정 디렉토리/폴더의 일반 파일에 관심이 있습니까? – Levon
나는이 질문이 네살 인 것을 알고 있지만 아직 받아 들여지지 않은 대답을 가지고있다. – raoulcousins