# 1、logs目录下,有一部分文件是空的
# 1、删除log目录下,所有的空文件
# 2、删除5天前的文件 # 需求分析:
# 1、os.walk()获取到所在以.log结尾的文件
# 2、判断文件的大小,os.getsize()
# 3、先从文件件名里面获取到文件的日期,然后把日期转成时间戳
# 4、再获取到当年的时间戳,时行比较 import os,time,datetime
def strToTimestamp(str=None,format='%Y%m%d%H%M%S'):
# 20180421165643
#默认返回当前时间戳
if str: #如果传了时间的话
tp = time.strptime(str,format) #格式化好的时间,转成时间元组
res = time.mktime(tp)#再转成时间戳
else:
res = time.time() #默认取当前的时间戳
return int(res) def clean_log(path):
for abs_path,dir,file in os.walk(path):
for f in file:
if f.endswith('.log'):
full_path = os.path.join(abs_path,f) #文件的绝对理解
f_date = f.split('_')[-1].split('.')[0] #文件名里面的日期
f_date_time_stamp = strToTimestamp(f_date,'%Y-%m-%d') #把文件名里面的日期转成时间戳
five_day = str(datetime.date.today() + datetime.timedelta(-5)) #获取到5天前的日期
five_day_time_stamp = strToTimestamp(five_day, '%Y-%m-%d') # 再把天前的日期转成时间戳
if os.path.getsize(full_path)==0 or f_date_time_stamp<five_day_time_stamp:
os.remove(full_path)
clean_log('logs')

操作LOG文件-删除log目录下,所有的空文件-删除5天前的文件的更多相关文章

  1. Python批量删除指定目录下的指定类型的文件

    Python作为一种脚本语言.其很适合文件级的各种操作.以下的代码能够批量删除指定目录下的所有特定类型(CSV类型)的文件. import sys, csv , operator import os ...

  2. linux面试题:删除一个目录下的所有文件,但保留一个指定文件

    面试题:删除一个目录下的所有文件,但保留一个指定文件 解答: 假设这个目录是/xx/,里面有file1,file2,file3..file10 十个文件 [root@oldboy xx]# touch ...

  3. Linux:删除一个目录下的所有文件,但保留一个指定文件

    面试题:删除一个目录下的所有文件,但保留一个指定文件 解答: 假设这个目录是/xx/,里面有file1,file2,file3..file10  十个文件 [root@oldboy xx]# touc ...

  4. [No000073]C#直接删除指定目录下的所有文件及文件夹(保留目录)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  5. [ Windows BAT Script ] 删除某个目录下的所有某类文件

    删除某个目录下的所有某类文件 @echo off for /R %%s in (*.txt) do ( echo %%s del %%s ) pause @echo on

  6. python 删除一个目录下的所有文件

    一个目录下有文件,文件夹,文件夹里又有文件.文件夹....用python脚本,实现,递归删除一个目录下的所有文件: 目录结构如下: 其中我们要删除所有文件 代码实现如下: import os CUR_ ...

  7. linux --> 删除指定目录下所有文件

    删除指定目录下所有文件 代码样例: ///////////////////////////////////////////////////// //Name: DeleteFile //Purpose ...

  8. C# 删除指定目录下的所有文件及文件夹

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...

  9. PHP删除一个目录下的所有文件,不删除文件夹

    /*删除指定目录下的文件,不删除目录文件夹*/ function delFile($dirName){ if(file_exists($dirName) && $handle=open ...

  10. SVN 删除所有目录下的“.svn”文件夹,让文件夹脱离SVN控制

    SVN 删除所有目录下的“.svn”文件夹,将如下语句拷备到记事本,并保存为 *.reg,双击导入注册表,在文件夹右键中就多了一条“Delete SVN Folders”,点击就可以删处此目录下的所有 ...

随机推荐

  1. Git 使用指南(cmd + gui)

    git 使用简易指南http://www.bootcss.com/p/git-guide/ Git版本控制使用方法入门教程http://www.uml.org.cn/pzgl/201204285.as ...

  2. CodeForces 450B Jzzhu and Sequences 【矩阵快速幂】

    Jzzhu has invented a kind of sequences, they meet the following property: You are given x and y, ple ...

  3. Good Bye 2016 A. New Year and Hurry【贪心/做题目每道题花费时间按步长为5等差增长,求剩余时间够做几道题】

    A. New Year and Hurry time limit per test 1 second memory limit per test 256 megabytes input standar ...

  4. A/B Testing with Practice in Python (Part One)

    I learned A/B testing from a Youtube vedio. The link is https://www.youtube.com/watch?v=Bu7OqjYk0jM. ...

  5. hdu6035(树形DP)

    hdu6035 题意 给出一棵树,现在定义两点之间距离为两点间最短路径上颜色集合的大小.问任意两点间距离之和. 分析 换个方向,题目其实等价于求每种颜色在多少条路径上出现过(每种颜色对于答案的贡献), ...

  6. 使用CXF开发RESTFul服务

    相信大家在阅读CXF官方文档(http://cxf.apache.org/docs/index.html)时,总是一知半解.这里向大家推荐一本PacktPub.Apache.CXF.Web.Servi ...

  7. JD2

    Business Requirement Support l Develops and communicates plan to manage vendor review of requirement ...

  8. 收纳箱1号 | GitHub Pages部署静态网页的一点私货

    Static site 总结各种各有的 static site generator Jekyll 其实是一个 static site generator. 如果你去 Google 这个,会发现有很多总 ...

  9. Jackson反序列JSON为实体对象出现:no String-argument constructor/factory method to deserialize from String value的问题

    解决方法: 1.JSON字符串中有转义字符,可以替换,也可以直接toString之后清除转移字符. 参考: https://stackoverflow.com/questions/40986738/s ...

  10. !!!!Linux系统开发 系列 4 进程资源 环境 fork()子进程 wait() waitpid()僵尸 孤儿进程

    http://990487026.blog.51cto.com/10133282/1834893