欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python刪除過期log文件操作實(shí)例解析

 更新時(shí)間:2018年01月31日 09:08:35   作者:y2701310012  
這篇文章主要介紹了python刪除過期log文件,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下

本文研究的主要是python刪除過期log文件的相關(guān)內(nèi)容,具體介紹如下。

1. 用Python遍歷目錄

os.walk方法可以很方便的得到目錄下的所有文件,會返回一個(gè)三元的tupple(dirpath, dirnames, filenames),其中,dirpath是代表目錄的路徑,dirnames是一個(gè)list,包含了dirpath下的所有子目錄的名字,filenames是一個(gè)list,包含了非目錄的文件,如果需要得到全路徑,需要使用os.path.join(dirpath,name).例如test目錄的結(jié)構(gòu)為:

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 

結(jié)果為:

('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) 

結(jié)果為:

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

當(dāng)然,也可以利用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. 排除需要保留文件

根據(jù)特定名稱的文件以及文件更改時(shí)間來判斷是否需要刪除,os.path.getmtime(file)來得到文件最后改變的時(shí)間,當(dāng)然除了諸如“XXX" in file的方法來判斷文件名外,也可以采用正則表達(dá)式的方法。

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定時(shí)每天執(zhí)行一次,即可定期每天清理/var/server/log下的過期文件。

總結(jié)

以上就是本文關(guān)于python刪除過期log文件操作實(shí)例解析的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!

相關(guān)文章

  • Python創(chuàng)建字典的八種方式

    Python創(chuàng)建字典的八種方式

    今天小編就為大家分享一篇關(guān)于Python創(chuàng)建字典的八種方式,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-02-02
  • Python實(shí)現(xiàn)雙色球號碼隨機(jī)生成

    Python實(shí)現(xiàn)雙色球號碼隨機(jī)生成

    和體彩大樂透類似,福彩雙色球也是購買次數(shù)最多的彩種之一,相比大樂透,雙色球更容易中小獎。本文將介紹?Python?實(shí)習(xí)雙色球彩票自由的流程,感興趣的可以了解一下
    2022-05-05
  • 解決echarts中餅圖標(biāo)簽重疊的問題

    解決echarts中餅圖標(biāo)簽重疊的問題

    這篇文章主要介紹了解決echarts中餅圖標(biāo)簽重疊的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-05-05
  • Python-opencv實(shí)現(xiàn)紅綠兩色識別操作

    Python-opencv實(shí)現(xiàn)紅綠兩色識別操作

    這篇文章主要介紹了Python-opencv實(shí)現(xiàn)紅綠兩色識別操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • 聊聊python中的循環(huán)遍歷

    聊聊python中的循環(huán)遍歷

    這篇文章主要介紹了python中的循環(huán)遍歷的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下
    2020-09-09
  • pandas的唯一值、值計(jì)數(shù)以及成員資格的示例

    pandas的唯一值、值計(jì)數(shù)以及成員資格的示例

    今天小編就為大家分享一篇pandas的唯一值、值計(jì)數(shù)以及成員資格的示例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • python regex庫實(shí)例用法總結(jié)

    python regex庫實(shí)例用法總結(jié)

    在本篇內(nèi)容里小編給大家整理了關(guān)于python regex庫實(shí)例用法總結(jié)內(nèi)容,有需要的朋友們參考學(xué)習(xí)下。
    2021-01-01
  • spyder 在控制臺(console)執(zhí)行python文件,debug python程序方式

    spyder 在控制臺(console)執(zhí)行python文件,debug python程序方式

    這篇文章主要介紹了spyder 在控制臺(console)執(zhí)行python文件,debug python程序方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • VS2019+python3.7+opencv4.1+tensorflow1.13配置詳解

    VS2019+python3.7+opencv4.1+tensorflow1.13配置詳解

    這篇文章主要介紹了VS2019+python3.7+opencv4.1+tensorflow1.13配置詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • Python+Selenium定位不到元素常見原因及解決辦法(報(bào):NoSuchElementException)

    Python+Selenium定位不到元素常見原因及解決辦法(報(bào):NoSuchElementException)

    這篇文章主要介紹了Python+Selenium定位不到元素常見原因及解決辦法(報(bào):NoSuchElementException),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03

最新評論