python-正則表達式匹配文件名,將以特定字符串開頭的文件刪除,並記錄txt格式刪除日志


 

  oracle數據庫會生成core.開頭的日志文件,如果不定期刪除就會占用過多系統內存;

  所以寫腳本輪詢文件名,re.match匹配特定格式字符串開頭文件,用isExists判斷文件是否存在,如果存在用delete_file函數刪除,並寫入fileDelete_coreLog.txt文件中。

 

#!/usr/bin/python
# -*- coding: utf-8 -*-

import os
import re
import shutil
 
def  delete_file(pos):
    pos = str(pos)
    os.system('rm /home/*%s*'  % (pos))
 
def check_flie(filePath,aimfile):
    for i,j,k in os.walk(filePath):
        n = 0
        while n < len(k):
            ret = re.match("core\.*",k[n])  
            if ret:
                isExists=os.path.exists(filePath + '//' + k[n])
                if not isExists:
                    pass
                else:
                    with open(aimfile, 'a+') as f:
                        f.write(filePath + '/' + k[n] + '      has been deleted!!\n')
                        f.close
                        delete_file(k[n])
            n = n + 1
        
aimfile = '/home/fileDelete_coreLog.txt'        
filePath = '/home'
check_flie(filePath,aimfile)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM