파이썬에서는 현재 디렉토리의 모든 파일 만 나열하고 싶습니다. 하위 디렉토리 또는 상위 디렉토리에서 파일을 나열하지 않습니다.
비슷한 솔루션이있는 것 같지만 저에게는 효과가없는 것 같습니다. 내 코드 스 니펫은 다음과 같습니다.
import os
for subdir, dirs, files in os.walk('./'):
for file in files:
do some stuff
print file
현재 디렉토리에 holygrail.py와 Tim이라는 두 개의 파일이 있다고 가정 해 봅시다. 폴더도 있고 그 안에 파일 두 개 (Arthur와 Lancelot이라고 함)를 포함합니다. 스크립트를 실행하면 다음과 같은 결과가 나타납니다.
holygrail.py
Tim
Arthur
Lancelot
holygrail.py와 Tim에 만족합니다. 그러나 Arthur와 Lancelot이라는 두 파일은 나열하고 싶지 않습니다.
[os.path.join(path_base,f) for f in os.listdir(path_base) if os.path.isfile(os.path.join(path_base,f))]