python中把压缩的Log解压,并查询其中的某个值出现的次数

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}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值