본문 바로가기
python3 selenium/Folder , File

파이썬 Python 특정 폴더 내 모든 파일 가져오기 (하위폴더 제외)

by Pymac 2025. 5. 5.
반응형

# 파이썬으로 특정 폴더 내 모든 파일 가져오기 (하위폴더 제외)

 

파이썬의 `os` 모듈을 사용하면 특정 폴더 안에 있는 파일들을 쉽게 불러올 수 있습니다.  

다음 예제는 **하위 폴더를 제외하고**, 폴더 안의 **파일만** 가져옵니다.

 

import os

 

folder_path = 'C:/Users/username/Documents/myfolder'

 

# 파일만 필터링

all_files = [f for f in os.listdir(folder_path)

             if os.path.isfile(os.path.join(folder_path, f))]

 

print(all_files)

 

os.listdir()은 폴더 내부의 모든 항목(파일 + 폴더)을 나열하고,

os.path.isfile()로 파일만 걸러냅니다.

 

반응형