반응형 python3 selenium/Folder , File15 파이썬 Python glob으로 특정 확장자 파일 가져오기 # glob으로 특정 확장자 파일 가져오기 `glob` 모듈을 사용하면 와일드카드로 쉽게 파일을 검색할 수 있습니다. import glob folder_path = 'C:/Users/username/Documents/myfolder' # .txt 파일만 가져오기txt_files = glob.glob(f"{folder_path}/*.txt") print(txt_files) *.txt는 해당 폴더에서 .txt 확장자를 가진 파일을 의미합니다. 2025. 5. 5. 파이썬 Python 특정 확장자 파일만 가져오기 # 파이썬으로 특정 확장자 파일만 가져오기 (.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 등 원하는 확장자로 바꿔서 사용할 수 있어요. 2025. 5. 5. 파이썬 Python 특정 폴더 내 모든 파일 가져오기 (하위폴더 제외) # 파이썬으로 특정 폴더 내 모든 파일 가져오기 (하위폴더 제외) 파이썬의 `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()로 파일만 걸러.. 2025. 5. 5. 이전 1 2 3 다음 반응형