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

原文地址:https://www.cnblogs.com/xzlive/p/9439409.html

时间: 2024-08-01 08:10:20

python 删除2天前后缀为.log的文件的相关文章

python删除指定日期前的备份文件

这个脚本还有待完善,没有加入迭代子目录,后期完善. #!/bin/env python #coding=utf-8 import time,datetime,os,sys dir='/usr/local/src' #被删除文件写入日志文件 logdir='/var/log' logfile=os.path.join(logdir,'delete.log') # 定义时间格式 time_format = "%a %b %d %H:%M:%S %Y" # 函数返回"2015-02

python删除N天前文件

#/usr/bin/env python # -*- coding:utf-8 -*- #Author='Jun' import os import sys import time def removefiles(beftime, defalutpath='.'):     for i in os.listdir(path):         filename = path + os.sep + i         if os.path.getmtime(filename) < beftime:

python 删除多个同一后缀名文件(基于python 3.X)

import os def remove(): filearray = [] address_Excel="E:\\totally\\FinancePDF" f_list = os.listdir(address_Excel) for fileNAME in f_list: # os.path.splitext():分离文件名与扩展名 if os.path.splitext(fileNAME)[1] == '.xlsx': filearray.append(fileNAME ) # 以

python 删除/app/*/logs/*/*.logs指定多少天的文件

# encoding: utf-8 import sys import getopt import os import glob import time import datetime def removefile(keep=7): for file in glob.glob('/app/*/logs/*/*.log'): # 获取当前时间 today = datetime.datetime.now() # 计算偏移量,前k天 offset = datetime.timedelta(minute

python脚本删除n天前文件可用于windows,linux并且支持跨平台

脚本如下: #!/usr/local/python/bin/python # -*-coding=utf8 -*- import time import os, sys # 设置删除多少天前的文件 N = 3 #要删除路径 path = r'/tmp/wry/abc' def deletefile(path): for eachfile in os.listdir(path): filename = os.path.join(path, eachfile) if os.path.isfile(f

linux下用shell删除三天前或者三天内的文件

说明:+n 大于 n, -n 小于 n, n 相等于 n. find / -amin -30 -ls # 查找在系统中最后30分钟访问的文件find / -atime -2 -ls # 查找在系统中最后48小时访问的文件find / -mmin -10 -ls # 查找在系统中最后10分钟里修改过的文件find / -mtime -1 -ls # 查找在系统中最后24小时里修改过的文件find / -cmin -10 -ls # 查找在系统中最后10分钟里被改变状态的文件find / -ctim

shell删除三天前或者三天内的文件

说明:+n 大于 n, -n 小于 n, n 相等于 n. find / -amin -30 -ls # 查找在系统中最后30分钟访问的文件find / -atime -2 -ls # 查找在系统中最后48小时访问的文件find / -mmin -10 -ls # 查找在系统中最后10分钟里修改过的文件find / -mtime -1 -ls # 查找在系统中最后24小时里修改过的文件find / -cmin -10 -ls # 查找在系统中最后10分钟里被改变状态的文件find / -ctim

log4j删除N天前日志实现

基于Log4j完成定时创建和删除日志的方法 1.     背景 最近要实现定期删除N天前的日志需求: Log4j作为常用的日志生成工具,其清除日志的策略却十分有限.只有在RollingFileAppender中可以通过设置MaxFileSize和maxBackupIndex属性来指定要保留的日志文件大小以及个数,从而实现自动清除. 但是实际生产中,我们的真实的需求常常是定时每天生成一个日志文件,然后保留最近几天或近几个月的日志,历史日志需要及时清理.可是Log4j中的DailyRollingFi

oracle定时备份与删除N天前备份文件

oracle定时备份数据库,以及删除7天前备份的数据. 1.创建存放备份目录: mkdir /home/oracle/data_backup mkdir /home/oracle/log_backup 2.编辑脚本oracle_backup.sh,内容如下: #!/bin/sh export ORACLE_BASE=/home/oracleexport ORACLE_HOME=$ORACLE_BASE/product/10.2.0/db_1export ORACLE_SID=orclexport