python删除过期log文件操作实例解析

yipeiwu_com6年前Python基础

本文研究的主要是python删除过期log文件的相关内容,具体介绍如下。

1. 用Python遍历目录

os.walk方法可以很方便的得到目录下的所有文件,会返回一个三元的tupple(dirpath, dirnames, filenames),其中,dirpath是代表目录的路径,dirnames是一个list,包含了dirpath下的所有子目录的名字,filenames是一个list,包含了非目录的文件,如果需要得到全路径,需要使用os.path.join(dirpath,name).例如test目录的结构为:

test------------file_c
|
-----------dir_a1/file_a1
| |
| -------dir_a2/file_a2
|
------------dir_b1/file_b1

那么使用如下代码:

import os 
 
for i in os.walk('test'): 
   print i 

结果为:

('test', ['dir_a1', 'dir_b1'], ['file_c1'])('test/dir_a1', ['dir_a2'], ['file_a1'])('test/dir_a1/dir_a2', [], ['file_a2'])('test/dir_b1', [], ['file_b1'])

要得到带路径的文件,则可以这样操作:

for i in os.walk('test'): 
   #print i 
   for j in i[2]: 
     os.path.join(i[0],j) 

结果为:

'test/file_c1'
'test/dir_a1/file_a1'
'test/dir_a1/dir_a2/file_a2'
'test/dir_b1/file_b1'

当然,也可以利用os.path.isdir判断来递归操作得到目录中的文件:

def walk(dir): 
  ret = [] 
  dir = os.path.abspath(dir) 
  for file in [file for file in os.listdir(dir) if not file in [".",".."]]: 
    nfile = os.path.join(dir,file) 
    if os.path.isdir(nfile): 
      ret.extend( walk(nfile) ) 
    else: 
      ret.append( nfile ) 
  return ret 

2. 排除需要保留文件

根据特定名称的文件以及文件更改时间来判断是否需要删除,os.path.getmtime(file)来得到文件最后改变的时间,当然除了诸如“XXX" in file的方法来判断文件名外,也可以采用正则表达式的方法。

def shouldkeep(file): 
  if '.py' in file: 
    return True 
  elif '.conf' in file: 
    return True 
  elif 'current' in file: 
    return True 
  elif 'rtb' in file and datetime.datetime.fromtimestamp( os.path.getmtime(file) ) > datetime.datetime.now() - datetime.timedelta(3): 
    return True 
  # the log webdebug/popterr/webaccess/controller_slow/game/checking_social which are modified 6 day ago should be removed 
  elif datetime.datetime.fromtimestamp( os.path.getmtime(file) ) < \ 
     datetime.datetime.now() - datetime.timedelta(6)\ 
     and ('webdebug' in file \ 
     or 'potperr' in file\ 
     or 'webaccess' in file\ 
     or 'controller_slow' in file\ 
     or 'game.' in file\ 
     or 'checkin_social' in file\ 
     ): 
    return False 
  elif datetime.datetime.fromtimestamp( os.path.getmtime(file) ) < \ 
     datetime.datetime.now() - datetime.timedelta(2)\ 
     and ('queue.master.info' in file): 
    return False 
  elif datetime.datetime.fromtimestamp( os.path.getmtime(file) ) > \ 
     datetime.datetime.now() - datetime.timedelta(6): 
    return True 
  else: 
    return False 
files = walk('/var/server/log') 
for i in files: 
  if not shouldkeep(i): 
    print i, datetime.datetime.fromtimestamp( os.path.getmtime(i) ) 
    os.remove( i ) 

将该脚本用crontab定时每天执行一次,即可定期每天清理/var/server/log下的过期文件。

总结

以上就是本文关于python删除过期log文件操作实例解析的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

相关文章

python tensorflow学习之识别单张图片的实现的示例

python tensorflow学习之识别单张图片的实现的示例

假设我们已经安装好了tensorflow。 一般在安装好tensorflow后,都会跑它的demo,而最常见的demo就是手写数字识别的demo,也就是mnist数据集。 然而我们仅仅是...

Python下opencv图像阈值处理的使用笔记

Python下opencv图像阈值处理的使用笔记

图像的阈值处理一般使得图像的像素值更单一、图像更简单。阈值可以分为全局性质的阈值,也可以分为局部性质的阈值,可以是单阈值的也可以是多阈值的。当然阈值越多是越复杂的。下面将介绍opencv...

详解python--模拟轮盘抽奖游戏

详解python--模拟轮盘抽奖游戏

题目: 轮盘分为三部分: 一等奖, 二等奖和三等奖; 轮盘转的时候是随机的, 如果范围在[0,0.08)之间,代表一等奖, 如果范围在[0.08,0.3)之间,代表2等奖, 如果范围在[...

Python中防止sql注入的方法详解

前言 大家应该都知道现在web漏洞之首莫过于sql了,不管使用哪种语言进行web后端开发,只要使用了关系型数据库,可能都会遇到sql注入攻击问题。那么在Python web开发的过程中s...

python中字典按键或键值排序的实现代码

字典排序 在程序中使用字典进行数据信息统计时,由于字典是无序的所以打印字典时内容也是无序的。因此,为了使统计得到的结果更方便查看需要进行排序。Python中字典的排序分为按“键”排序和...