zoukankan      html  css  js  c++  java
  • python删除指定目录下N天前的文件

    # -*- coding: utf-8 -*-
    import os
    import sys
    import time
    
    # Sets how many days old files are deleted
    
    # To delete the path and the following subfiles
    
    class DelLogUtil():
        def deletefile(self,PATH,DAYS_N):
            for eachfile in os.listdir(PATH):
                # print eachfile
                # 带路径的文件名 D:	mpfiles234.log
                filename = os.path.join(PATH, eachfile)
                # print filename
                if os.path.isfile(filename):
                    lastmodifytime = os.stat(filename).st_mtime
                    # print lastmodifytime
                    # Sets how many days old files are deleted
                    endfiletime = time.time() - 3600 * 24 * DAYS_N
                    if endfiletime > lastmodifytime:
                        # To remove the following comment is to delete the.log suffix file
                        # Comment is delete path under all files do not match
                        if filename[-4:] == ".log":
                            os.remove(filename)
                            print "del %s success!!!" % filename
                # If it is a directory, the current function is called recursively
                elif os.path.isdir(filename):
                    df = DelLogUtil()
                    df.deletefile(filename,DAYS_N)
    
    
    if __name__ == '__main__':
        df = DelLogUtil()
        df.deletefile(r'D:	mpfiles', 10)
    
    time.sleep(1)
    print ('Deleting completed,success')
  • 相关阅读:
    java实现同步的两种方式
    JAVA线程概念
    XML基础总结
    JAVA使用和操作properties文件
    JAVA序列化基础知识
    easyui 在编辑状态下,动态修改其他列值。
    Activiti初学问题,求解
    java web--DOM
    java web(1)
    Java WEB
  • 原文地址:https://www.cnblogs.com/yoyowin/p/12172315.html
Copyright © 2011-2022 走看看