[python]按文件创建时间清理文件

 1 import os
 2 import time
 3 root_dir = r'E:\I-SEARCH\考核追踪交付文档'
 4 def clear_file(root_dir):
 5     for root, dir, names in os.walk(root_dir):
 6         print(root, dir, names)
 7         for name in names:
 8             filename = os.path.join(root, name)
 9             print(filename)
10             #文件创建时间戳
11             ct = os.stat(filename).st_ctime
12             #当前时间戳
13             nt = time.time()
14             #文件创建时间
15             tt = nt - ct
16             #返回 分 秒
17             m, s = divmod(tt, 60)
18             #小时,分钟
19             h, m = divmod(m, 60)
20             #天,小时
21             d, h = divmod(h, 24)
22             if d >= 1:
23                 print("文件超过1天,执行清理")
24 clear_file(root_dir)

 

posted @ 2020-09-25 09:32  TD_1900  阅读(241)  评论(0)    收藏  举报