python 3.x - Searching for errors in a number of files -
new python have been seeking develop script allows me search errors in multiple files in 1 large folder. problem, think, @ moment code returns first error comes across in file , not seek find anymore. problematic have no idea how many, if there any, errors file contain.
this have written far:
# importing libraries import os os import listdir # !/usr/bin/env python # defining location of parent folder base_directory = 'folder' output_file = open('folder', 'w') output = {} file_list = [] # scanning through sub folders (dirpath, dirnames, filenames) in os.walk(base_directory): f in filenames: if '.log' in str(f): e = os.path.join(str(dirpath), str(f)) file_list.append(e) f in file_list: print (f) txtfile = open(f, 'r') output[f] = [] line in enumerate(txtfile): if 'error' in line: output[f].append(line) elif 'warning' in line: output[f].append(line) elif 'fatal' in line: output[f].append(line) tabs = [] tab in output: tabs.append(tab) tabs.sort() tab in tabs: output_file.write(tab + '\n') output_file.write('\n') row in output[tab]: output_file.write(row + '') output_file.write('\n') output_file.write('----------------------------------------------------------\n') input()
any appreciated.
Comments
Post a Comment