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

파이썬 Python 파일명 패턴이 숫자 또는 날짜 형식인 파일만 필터링

by Pymac 2025. 5. 5.
반응형

# 파일명 패턴이 숫자 또는 날짜 형식인 파일만 필터링

 

파일명이 `20240505_report.txt` 처럼 날짜/숫자 패턴을 포함할 경우 정규식을 사용할 수 있습니다.

 

import os

import re

 

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

 

pattern = re.compile(r'\d{8}_report\.txt')  # 예: 20240505_report.txt

 

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

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

 

print(matched_files)

 

날짜 형식이나 넘버링된 백업 파일 등을 다룰 때 유용합니다.

반응형