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)