温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python怎么删除过期log文件

发布时间:2021-04-21 10:11:28 来源:亿速云 阅读:224 作者:小新 栏目:开发技术

这篇文章主要介绍python怎么删除过期log文件,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

python是什么意思

Python是一种跨平台的、具有解释性、编译性、互动性和面向对象的脚本语言,其最初的设计是用于编写自动化脚本,随着版本的不断更新和新功能的添加,常用于用于开发独立的项目和大型项目。

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文件”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI