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