【Python】ディレクトリ内で最も古いCSVファイルを削除する方法。
ソースコードは以下のとおりです。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from glob import glob | |
path = "dir/*.csv" | |
files = glob(path) | |
if len(files) > 0: | |
files.sort(cmp=lambda x, y: int(os.path.getctime(x) - os.path.getctime(y))) | |
os.remove(files[0]) |