지정된 경로와 고정 된 깊이의 디렉토리 목록을 반환하는 기능을 원한다면 몇 가지 대안을 곧 실현할 수 있습니다. 나는 os.walk을 꽤 많이 사용하고 있지만 깊이를 계산할 때 코드가보기 싫어지기 시작했습니다.Python에서 지정된 깊이의 디렉토리 나열
실제로 가장 "깔끔한"구현은 무엇입니까?
import os,string
path = '.'
path = os.path.normpath(path)
res = []
for root,dirs,files in os.walk(path, topdown=True):
depth = root[len(path) + len(os.path.sep):].count(os.path.sep)
if depth == 2:
# We're currently two directories in, so all subdirs have depth 3
res += [os.path.join(root, d) for d in dirs]
dirs[:] = [] # Don't recurse any deeper
print(res)
glob를 사용한 첫 번째 예는 훌륭하고 나는 그 트랙을 본 적이 없었습니다. 대단히 감사합니다! – StefanE