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

파이썬 Python 특정 확장자 파일만 가져오기

by Pymac 2025. 5. 5.
반응형

# 파이썬으로 특정 확장자 파일만 가져오기 (.txt 등)

 

특정 확장자만 가져오고 싶다면 `endswith()`를 활용하면 됩니다.

 

import os

 

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

ext = '.txt'

 

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

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

 

print(txt_files)

 

.txt 대신 .jpg, .csv 등 원하는 확장자로 바꿔서 사용할 수 있어요.

반응형