Python小工具-查找关键字

跑Monkey时,可能会产生大量日志,如果一个一个找的话又太浪费时间,所以通过Python编写了一段代码来查找需要的关键字,如果没有我们想要的关键字,就完全没有必要再去将日志翻一遍了

exc = ['Touch','CRASH','ANR','died']  # 要查找的关键字

with open('C:\\Users\\monkey_01.txt', encoding='utf-8') as f:    
    if os.path.exists('copy.txt'):                   
        with open('copy.txt','r+') as f2:    # 清空文件
            res = f2.readlines()
            print(res)
            f2.seek(0)
            f2.truncate()
    with open('copy.txt', 'at') as f1:       
        f1.write('崩溃日志:\n')
    dic_key = {}
    for key in exc:
        dic_key[key] = 0
    for line in f:
        for key in exc:
            if key in line:
                dic_key[key] =dic_key[key] + line.count(key)
                # print(line)
                with open('copy.txt','at') as f:
                    f.write(line)
    # print(dic_key)
    with open('copy.txt','at') as f:                  # 写入文件
        f.write("分析结果:\n")
        f.write("------------------------------\n")
        for key, value in dic_key.items() :
            f.write("关键字{}出现的次数:{}\n".format(key, value))

 

posted @ 2019-07-04 19:24  山重水复疑无路  阅读(2224)  评论(0编辑  收藏  举报