python中把压缩log解压并查询其中的某个值
import os
import zipfile
import time
import filetype
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
def unzip(file_name):
target_path = f"./log_{file_name}"
log_list = []
dup_list = []
# tmp = [["./",file_name]]
tmp = [file_name]
while len(tmp) > 0:
head = tmp.pop()
# print(f"head is {head} ")
file_kind = filetype.guess(head)
if file_kind != None and file_kind.extension == "zip":
fz = zipfile.ZipFile(head, "r")
for file_path in fz.namelist():
# file_kind = filetype.guess(head)
# print(f"wait ext :{file_path}")
fz.extract(file_path, path=target_path)
if file_path.startswith("/"):
file_path = f"{target_path}" + file_path
file_path = f"." + file_path
file_path = os.path.join(target_path, file_path)
tmp.append(file_path)
elif head.endswith(".log"):
log_list.append(head)
else:
dup_list.append(head)
# print(log_list)
# print(dup_list)
return log_list, dup_list
# 日志字符串查询1214220,206697,646631
def findstr(file_path, find_str):
dict_number = {}
list_address = []
for item in find_str:
dict_number[item] = 0
try:
log_address, log_file = unzip(file_path)
except Exception as e:
print('没有找到log文件')
else:
for item in log_address:
list_address.append(item)
with open(item, 'r') as f:
while True:
try:
logfile = f.readline()
except Exception as e:
pass
else:
if logfile: # 判断读取的行是否有值
for items in dict_number.keys(): # 循环需要查询的标签
dict_number[items] += logfile.count('t:' + items) # 查询标签出现的次数
else:
break
print({'appear_number': dict_number, 'log_address': list_address})
return {'appear_number': dict_number, 'log_address': list_address}