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
python 删除2天前后缀为.log的文件的更多相关文章
- 使用python删除N天前的文件
python版本为:2.7 import os import sys import time # Sets how many days old files are deleted DAYS_N = 7 ...
- python 删除三天前的日志.py
#获取所有文件def file(): for cur_dir, dirs, files in os.walk(r'/学习/接口自动化/BestTest/作业/logs'): # cur_dir(当前路 ...
- python删除x天前文件及文件夹
#!/usr/bin/env python # -*- coding:utf-8 -*- import os, time, sys, shutil def delFiles(beforeSec, di ...
- [Python] 删除指定目录下后缀为 xxx 的过期文件
import os import time import datetime def should_remove(path, pattern, days): if not path.endswith(p ...
- 删除linux上7天前后缀名.sql的文件
#!/bin/bash#delete the file of 7 days agofind /data/mysqlbackup/ -mtime +7 -name "*.sql" - ...
- python 删除/app/*/logs/*/*.logs指定多少天的文件
# encoding: utf-8 import sys import getopt import os import glob import time import datetime def rem ...
- 删除n天前的所有目录和文件
删除目录 find /your_dir/ -maxdepth -type d -mtime + -exec rm -rf {} \; 删除文件 find /目录/ -mtime + -name &qu ...
- UNIX命令,统计当前目录(含子目录)下所有后缀为.log的文件中ERROR出现的行数
shell程序如下所示: # cat xarg.txt #! /usr/bin/ksh for logfile in `find . -name "*.log*"` do echo ...
- 删除3天前创建的以log结尾的文件
1. #/bin/bash # filename: del_log.sh find / -name "*.log" -mtime 3 | xargs rm -rf 2. #/bin ...
随机推荐
- service手动实例化(new)导致类中的spring对象无法注入的问题解决
下面说的这个画横线的可能是错误的,因为我之前用controller继承父类的注解对象的时候成功了,所以可能这次的唯一原因就是 不该把本该从ioc容器中拿出的对象通过new的方式实例化,至于继承注解对象 ...
- SQL 追踪
SQL追踪(SQL Trace)是一个轻量级的追踪工具,按照事件(Events)记录数据库发生的消息,几乎对数据库性能没有什么影响.SQL Server内置一个Trace,称作默认追踪(Default ...
- 真机调试傻瓜图文教程(Xcode6.4)
先准备好99刀,真机调试才带你玩. PS:万能宝十来块钱可以买个资格... Developer Apple上的设置 1.打开https://developer.apple.com/,点击Member ...
- 如何设计一个异步Web服务——任务调度
接上一篇<如何设计一个异步Web服务——接口部分> Application已经将任务信息发到了Service服务器中,接下来,Service服务器改如何对自身的资源进行合理分配以满足App ...
- Jq_input file标签上传图片到服务器
引入jQuery库引入ajaxfileupload.js上传插件库(这也是jQuery的一个插件)以ASP.NET为例 <input type="file" id=" ...
- Unity2D 面向目标方向
在2d空间上,假设角色的自身的y轴方向为正方向,如果要让角色随时面向一个目标点. 这里假设(0,0)点为目标点 第一种: Vector3 v = Vector3.zero - transform.po ...
- PAT甲题题解-1017. Queueing at Bank (25)-模拟
有n个客户和k个窗口,给出n个客户的到达时间和需要的时长有空闲的窗口就去办理,没有的话就需要等待,求客户的平均时长.如果在8点前来的,就需要等到8点.如果17点以后来的,则不会被服务,无需考虑. 按客 ...
- PAT甲题题解-1059. Prime Factors (25)-素数筛选法
用素数筛选法即可. 范围long int,其实大小范围和int一样,一开始以为是指long long,想这就麻烦了该怎么弄. 而现在其实就是int的范围,那难度档次就不一样了,瞬间变成水题一枚,因为i ...
- LeetCode 551. Student Attendance Record I (C++)
题目: You are given a string representing an attendance record for a student. The record only contains ...
- ElasticSearch 2 (15) - 深入搜索系列之多字段搜索
ElasticSearch 2 (15) - 深入搜索系列之多字段搜索 摘要 查询很少是简单的一句话匹配(one-clause match)查询.很多时候,我们需要用相同或不同的字符串查询1个或多个字 ...