[파이썬] os.walk()함수를 사용하여 하위 디렉토리(파일) 를 검색하는 방법에 대해 알아보자
os모듈을 import 후 os.walk()함수를 사용하면 하위 디렉토리 안에 있는 모든 파일과 디렉토리 정보를 확인할 수 있습니다.
테스트를 위해 먼저 테스트 경로의 파일 및 디렉토리 정보를 확인하고 갑니다.
C:\python\test>dir
C 드라이브의 볼륨에는 이름이 없습니다.
볼륨 일련 번호: 1C02-20EB
C:\python\test 디렉터리
2020-12-07 오전 09:50 <DIR> .
2020-12-07 오전 09:50 <DIR> ..
2020-12-07 오전 10:16 <DIR> folder1
2020-12-07 오전 09:47 <DIR> folder2
2020-12-01 오후 02:38 277 newfile.txt
2020-12-01 오후 02:38 277 sample.txt
2020-12-01 오후 02:38 277 새파일.txt
3개 파일 831 바이트
4개 디렉터리 25,219,117,056 바이트 남음
C:\python\test>dir c:\python\test\folder1
C 드라이브의 볼륨에는 이름이 없습니다.
볼륨 일련 번호: 1C02-20EB
c:\python\test\folder1 디렉터리
2020-12-07 오전 10:16 <DIR> .
2020-12-07 오전 10:16 <DIR> ..
2020-12-01 오후 02:38 277 newfile_22222.txt
2020-12-01 오후 02:38 277 sample - 복사본.txt
2020-12-01 오후 02:38 277 sample.txt
3개 파일 831 바이트
2개 디렉터리 25,219,117,056 바이트 남음
C:\python\test>dir c:\python\test\folder2
C 드라이브의 볼륨에는 이름이 없습니다.
볼륨 일련 번호: 1C02-20EB
c:\python\test\folder2 디렉터리
2020-12-07 오전 09:47 <DIR> .
2020-12-07 오전 09:47 <DIR> ..
2020-11-30 오후 04:50 24,130 android_logo.png
2020-11-30 오후 04:48 85,872 java_logo.png
2020-11-30 오후 04:52 24,091 kotlin_logo.png
2020-12-01 오후 02:38 277 newfile.txt
2020-11-30 오후 03:01 42,100 python_logo.png
2020-12-01 오후 02:38 277 sample.txt
2020-11-30 오후 05:15 77,462 xbox_logo.png
7개 파일 254,209 바이트
2개 디렉터리 25,219,117,056 바이트 남음
C:\python\test>
테스트를 진행할 경로는 c:\python\test 입니다.
for문으로 os.walk() 결과값을 받기위해 3개의 변수를 대입합니다. 3개의 데이터를 전달받을 수 있기때문입니다.
각각 경로 정보와 디렉토리정보 그리고 파일 정보를 받을 수 있습니다.
import os
for path, dirs, files in os.walk("c:\python/test"):
#print(path, dir, files)
print('-' * 30)
print(path)
print('-' * 30)
print(dirs)
print('-' * 30)
print(files)
#실행결과
C:\Users\ilike\AppData\Local\Programs\Python\Python39\python.exe C:/python/Workspace/main.py
------------------------------
c:\python/test
------------------------------
['folder1', 'folder2']
------------------------------
['newfile.txt', 'sample.txt', '새파일.txt']
------------------------------
c:\python/test\folder1
------------------------------
[]
------------------------------
['newfile_22222.txt', 'sample - 복사본.txt', 'sample.txt']
------------------------------
c:\python/test\folder2
------------------------------
[]
------------------------------
['android_logo.png', 'java_logo.png', 'kotlin_logo.png', 'newfile.txt', 'python_logo.png', 'sample.txt', 'xbox_logo.png']
import os
for path, dirs, files in os.walk("c:\python/test"):
print(path, dir, files)
#실행결과
C:\Users\ilike\AppData\Local\Programs\Python\Python39\python.exe C:/python/Workspace/main.py
c:\python/test <built-in function dir> ['newfile.txt', 'sample.txt', '새파일.txt']
c:\python/test\folder1 <built-in function dir> ['newfile_22222.txt', 'sample - 복사본.txt', 'sample.txt']
c:\python/test\folder2 <built-in function dir> ['android_logo.png', 'java_logo.png', 'kotlin_logo.png', 'newfile.txt', 'python_logo.png', 'sample.txt', 'xbox_logo.png']
Process finished with exit code 0
os.path.join() 함수를 사용하여 경로 정보를 연결하여 결합한 결과를 얻을 수 있습니다.
import os
path2 = os.path.dirname("c:\python/test/")
print(path2)
for path, dirs, files in os.walk(path2):
print(os.path.join(path2, path))
#실행결과
C:\Users\ilike\AppData\Local\Programs\Python\Python39\python.exe C:/python/Workspace/main.py
c:\python/test
c:\python/test
c:\python/test\folder1
c:\python/test\folder2
이번에는 모든 디렉토리와 파일의 경로를 os.path.join()함수를 사용하여 출력해봅니다.
import os
path2 = os.path.dirname("c:\python/test/")
print(path2)
for path, dirs, files in os.walk(path2):
print(os.path.join(path2, path))
for file in files:
file_path = os.path.join(path, file)
print(file_path)
#실행결과
c:\python/test
c:\python/test
c:\python/test\newfile.txt
c:\python/test\sample.txt
c:\python/test\새파일.txt
c:\python/test\folder1
c:\python/test\folder1\newfile_22222.txt
c:\python/test\folder1\sample - 복사본.txt
c:\python/test\folder1\sample.txt
c:\python/test\folder2
c:\python/test\folder2\android_logo.png
c:\python/test\folder2\java_logo.png
c:\python/test\folder2\kotlin_logo.png
c:\python/test\folder2\newfile.txt
c:\python/test\folder2\python_logo.png
c:\python/test\folder2\sample.txt
c:\python/test\folder2\xbox_logo.png
