[Python] 하위 디렉토리 탐색 (sub-directory searching)

목적: 특정 디렉토리의 모든 하위 디렉토리 탐색

환경: Linux 3.10 (Centos)

Python Version: 3.6

#Searching all sub directories
import os

#Change the path ('/home/mkblog/test') inside the walk function
#[0]: directory path (PWD)
dirPwd = next(os.walk('/home/mkblog/test'))[0]
#[1]: all sub-directories 
dirList = next(os.walk('/home/mkblog/test'))[1]
#[2]: all sub-files
dirFiles = next(os.walk('/home/mkblog/test'))[2]

print(dirPwd);
print(dirList);
print(dirFiles);

설명

  • next의 [0]번 결과는 특정 디렉토리의 위치 값을 저장
  • next의 [1]번 결과는 특정 디렉토리의 모든 하위 디렉토리 리스트를 저장
  • next의 [2]번 결과는 특정 디렉토리에 포함된 모든 파일 리스트를 저장

 

Leave a Comment