zoukankan      html  css  js  c++  java
  • python脚本 – 删除指定天数前的文件

    # -*- coding: utf-8 -*-
    # Author: Jack
    # Date: 2018/02/09 15:33
    
    import os
    import sys
    import time
    
    # 删除某个时间点之前的文件
    def del_files(before_times, path=”.”):
        for root, dirs, files in os.walk(path):
            for f in files:
                full__path_file = os.path.join(root, f)
                if os.path.getmtime(full__path_file) < before_times:
                    try:
                        os.remove(full__path_file)
                        print(“[Succeed]: %s delete success.” % full__path_file)
                    except Exception as error:
                        print(“[Failed]: %s delete failed,%s” % (full__path_file, error))
    
    if __name__ == “__main__”:
        try:
            del_dir = “D:\\test3”         # 要操作删除的文件夹
            before_days = int(1)          #  设定删除几天前的文件
            before_time = time.time() – 3600 * 24 * before_days
            del_files(before_time, del_dir)
        except Exception as e:
            print(“py script error:%s” % e)
            sys.exit(-1) 
    
  • 相关阅读:
    浅谈生成全排列的4种方法
    UVA
    UVA
    UVA
    UVA
    MySQL索引篇
    MySQL事务篇
    MySQL架构篇
    Redis性能调优
    Redis分布式锁
  • 原文地址:https://www.cnblogs.com/interdrp/p/15569417.html
Copyright © 2011-2022 走看看