zoukankan      html  css  js  c++  java
  • python 删除2天前后缀为.log的文件

    python脚本 删除2天前后缀为.log的文件

    #!/usr/local/python/bin/python
    #-*-coding=utf8 -*-
    import time
    import os,sys
    N = 2 #设置删除多少天前的文件
    def deletefile(path):
            for eachfile in os.listdir(path):
                    filename = os.path.join(path,eachfile)
                    if os.path.isfile(filename):
                                lastmodifytime = os.stat(filename).st_mtime
                                endfiletime = time.time() - 3600 * 24 * N #设置删除多久之前的文件
                                if endfiletime > lastmodifytime:
                                    if filename[-4:]==".log":
                                        os.remove(filename)
                                        #print "删除文件 %s 成功" % filename
                    elif os.path.isdir(filename):#如果是目录则递归调用当前函数
                            deletefile(filename)
    
    if __name__ == '__main__':
            path = r'/home/logs/tomcat/'
            deletefile(path)
    

     设置crontab 任务

    10 0 * * * root python /home/shell/del_log.py 
    
  • 相关阅读:
    四十八.监控概述 、 Zabbix基础 、 Zabbix监控服务
    123D
    bzoj3879
    bzoj1699
    LA6878
    uoj#149
    687C
    codeforces round #424 div2
    803E
    713C
  • 原文地址:https://www.cnblogs.com/xzlive/p/9439409.html
Copyright © 2011-2022 走看看