标签:class with 正则 sts 生成 txt util 数据 utf-8
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 delete!!\n‘) f.close delete_file(k[n]) n = n + 1 aimfile = ‘/home/fileDelete_coreLog.txt‘ filePath = ‘/home‘ check_flie(filePath,aimfile)
python-正则表达式匹配文件名,将以特定字符串开头的文件删除,并记录txt格式删除日志
标签:class with 正则 sts 生成 txt util 数据 utf-8
原文地址:https://www.cnblogs.com/hydd/p/13558725.html