Python清理过期文件

改程序执行后,会清理 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)

 

转载于:https://www.cnblogs.com/delav/p/9936246.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值