python刪除到期log檔案操作執行個體解析,

來源:互聯網
上載者:User

python刪除到期log檔案操作執行個體解析,

本文研究的主要是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的方法來判斷檔案名稱外,也可以採用Regex的方法。

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檔案操作執行個體解析的全部內容,希望對大家有所協助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.