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中可迭代(Iterable)、迭代器(Iterator)与生成器(Generator)的概念

前言 在Python中可迭代(Iterable)、迭代器(Iterator)和生成器(Generator)这几个概念是经常用到的,初学时对这几个概念也是经常混淆,现在是时候把这几个概念搞...

在Django的模型中添加自定义方法的示例

为了给你的对像添加一个行级功能,那就定义一个自定义方法。 有鉴于manager经常被用来用一些整表操作(table-wide),模型方法应该只对特殊模型实例起作用。 这是一项在模型的一个...

Python产生Gnuplot绘图数据的方法

Python产生Gnuplot绘图数据的方法

gnuplot的绘图可以直接选取文件绘图,文件格式要求如下: x1 y1 x2 y2 ...... xn yn 在python中利用文件操作的write方法可以非常方便实现,在此记录一...

Python3.6 Schedule模块定时任务(实例讲解)

Python3.6 Schedule模块定时任务(实例讲解)

一,编程环境 PyCharm2016,Anaconda3 Python3.6 需要安装schedule模块,该模块网址:https://pypi.python.org/pypi/sche...

python实现ipsec开权限实例

本文实例讲述了python实现ipsec开权限的方法。分享给大家供大家参考。具体实现方法如下: windows自带的命令行工具netsh ipsec static add filter不...