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实现对比两张图片的不同

用python实现对比两张图片的不同

from PIL import Image from PIL import ImageChops def compare_images(path_one, path_two,...

用python制作游戏外挂

玩过电脑游戏的同学对于外挂肯定不陌生,但是你在用外挂的时候有没有想过如何做一个外挂呢?(当然用外挂不是那么道义哈,呵呵),那我们就来看一下如何用python来制作一个外挂。。。。 我打开...

python 中如何获取列表的索引

1.index方法 list_a= [12,213,22,2,32] for a in list_a: print(list_a.index(a)) 结果: 0 1 2 3...

python3+pyqt5+itchat微信定时发送消息的方法

python3+pyqt5+itchat微信定时发送消息的方法

编这个程序是想过节过年,一些重要的纪念日,给亲戚好友发祝福之类的,但要凌晨0点才显得比较有诚意,可我又比较贪睡,常常忘了,所以就有了编个微信定时发送消息小程序. 运行环境: python...

Python 模拟购物车的实例讲解

Python 模拟购物车的实例讲解

1.功能简介 此程序模拟用户登陆商城后购买商品操作。可实现用户登陆、商品购买、历史消费记查询、余额和消费信息更新等功能。首次登陆输入初始账户资金,后续登陆则从文件获取上次消费后的余额,每...