首页 > 编程语言 > 详细

Python清理过期文件

时间:2018-11-09 17:29:28      阅读:165      评论:0      收藏:0      [点我收藏+]

改程序执行后,会清理 test/文件夹中距离现在超过一天的以 .xml 结尾的文件

# coding: utf-8

import time
import os
root = os.path.dirname(__file__)
def clear_file(root_dir):
    print(333333)
    for root, dir, names in os.walk(root_dir):
        print(root, dir, names)
        for name in names:
            filename = os.path.join(root, name)
            ct = os.stat(filename).st_ctime
            nt = time.time()
            s1 = time.localtime(ct)
            s2 = time.localtime(nt)
            print "s%文件创建时间为%s", (name, time.strftime(%Y-%m-%d, s1))
            print "当前时间为:", time.strftime(%Y-%m-%d, s2)

            tt = nt - ct
            m, s = divmod(tt, 60)
            h, m = divmod(m, 60)
            d, h = divmod(h, 24)
            print "文件创建距离现在已%02d天%02d时%02d分%02d秒" % (d, h, m, s)
            if d >= 1 and name.endswith(.xml):
                print "文件超过一天,执行清理"
                os.remove(filename)
            elif d < 1 and m < 10 and name.endswith(.xml):
                os.remove(filename)

if __name__ == ‘__main__‘:
  root = root+/test/
  clear_file(root)

 

Python清理过期文件

原文:https://www.cnblogs.com/delav/p/9936246.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!