一键解压文件

import base64
import os
import tarfile
import gzip
import zipfile

# 对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)
    for f in os.listdir(dirs):
        l_file = dirs+"\\"+f
        with open(l_file, "r", encoding="utf-8" ,errors="ignore") as f: 
            for line in f:
                #if 'fatal' in line:
                    #print(line)
                #if 'died' in line:
                    #print(line)
                if 'avc: denied' in line and 'toybox_vendor' not in line:
                    print(line)
       

       

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

推荐阅读更多精彩内容