如何在python中删除旧文件并保留每个月的最后一天文件



我需要保留从4月31日开始到5月1日第二天结束的备份文件。每个文件夹的备份时间不同,但备份文件是相同的

这可能是解决问题的一种方法:os.path.getctime获得文件的创建日期,os.path.getmtime获得文件的最后修改日期

import os
if os.path.getctime("test.txt") > os.path.getctime("test2.txt"):
print("test.txt is more recent")
os.remove("test2.txt")
else:
print("test2.txt is more recent")
os.remove("test.txt")

相关内容

最新更新