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

파이썬 Python 수정 시간 기준으로 최신 파일 정렬하기

by Pymac 2025. 5. 5.
반응형

# 수정 시간 기준으로 최신 파일 정렬하기

 

가장 최근에 수정된 파일부터 정렬하고 싶을 때는 `os.path.getmtime()`을 사용하면 됩니다.

 

import os

 

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

 

file_with_time = [(f, os.path.getmtime(os.path.join(folder_path, f)))

                  for f in os.listdir(folder_path)

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

 

file_with_time.sort(key=lambda x: x[1], reverse=True)

 

sorted_files = [f[0] for f in file_with_time]

 

print(sorted_files)

 

파일 백업 또는 로그 확인 시 매우 유용합니다.

반응형