一键读取topmen

import base64
import os
import tarfile
import gzip
import zipfile
import re

# 对gz类型进行解压
def gzip_file(file):
    c_path = file+"\\logcat_log.tar.gz"
    try:
        g = gzip.GzipFile(mode='rb',fileobj=open(c_path,'rb'))
        open(c_path.replace('.gz',''),'wb').write(g.read())
    except Exception as e:
        print(e)
    else:
        untar(c_path.replace('.gz',''),file+'./',file)
        #print('文件解压成功!')
   
def untar(fname,dirs,origin):
    print("fname="+fname)
    try:
        t = tarfile.open(fname)
        t.extractall(path=dirs)
    except Exception as e:
        print(e)
    else:
        #print('解压tar成功')
        zip_allfile(origin+"\\log")

def zip_allfile(dirs):
    for f in os.listdir(dirs):
        if '.zip' in f:
            zip_file = zipfile.ZipFile(dirs+"\\"+f)
            zip_file.extract(zip_file.namelist()[0],dirs)
    index = 0
    for f in os.listdir(dirs):
        if 'topmen' in f:

            l_file = dirs+"\\"+f
            with open(l_file, "r", encoding="utf-8" ,errors="ignore") as L: 
                for line in L:
                    if '%CPU' in line:
                        index = 1
                    elif index >= 1:
                        index += 1
                        if index == 5:
                           index = 0
                        s_line = re.sub(' +',' ',line)
        
                        s_line = s_line.split(' ')
                       
                        data = [0,0,0,0,0]
                        if len(s_line) >= 12:
                            data[0] = s_line[2]
                            data[1] = s_line[6]
                            data[2] = s_line[7]
                            data[3] = s_line[9]
                            data[4] = l_file
                        if '.' in data[3]:
                            v = data[3][0:data[3].index('.')]
                            if int(v) > 40:
                               print(data)
                        else:
                            if int(v) > 40:
                               print(data)
                       # print(data)

                 
                        

if __name__ == "__main__":
    #modify_name()
    path = 'D:\\log分析\\log'
    zip_allfile(path)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容