python 删除大于超过一定时间文件

import os
from datetime import datetime, timedelta

path = "."
if __name__ == '__main__':
    for root, firs, files in os.walk(path):
        for file in files:
            if datetime.fromtimestamp(os.path.getctime(file)) < datetime.now() - timedelta(seconds=300):
                pass
                # os.remove(path + file)
            else:
                print(" cant not remove name : %s" % file)

posted @ 2023-02-22 10:01  vx_guanchaoguo0  阅读(54)  评论(0编辑  收藏  举报