python - How to parse logs and extract lines containing specific text strings? -


i've got several hundred log files need parse searching text strings. able run python script open every file in current folder, parse , record results in new file original_name_parsed_log_file.txt. had script working on single file i'm having issues doing files in directory.

below have far it's not working atm. disregard first def... playing around changing font colors.

import os import string ctypes import *  title = ' log parser ' windll.kernel32.getstdhandle.restype = c_ulong h = windll.kernel32.getstdhandle(c_ulong(0xfffffff5))  def display_title_bar():     windll.kernel32.setconsoletextattribute(h, 14)     print '\n'     print '*' * 75 + '\n'     windll.kernel32.setconsoletextattribute(h, 13)     print title.center(75, ' ')     windll.kernel32.setconsoletextattribute(h, 14)     print '\n' + '*' * 75 + '\n'      windll.kernel32.setconsoletextattribute(h, 11)  def parse_files(search):     filename in os.listdir(os.getcwd()):         newname=join(filename, '0_parsed_log_file.txt')         open(filename) read:             read.seek(0)            # search line values if found append line spaces replaced tabs new file.             open(newname, 'ab') write:                 line in read:                     val in search:                         if val in line:                             write.write(line.replace(' ', '\t'))                             line = line[5:]             read.close()             write.close() print'\n\n'+'parsing complete.' windll.kernel32.setconsoletextattribute(h, 15)  display_title_bar()  search = raw_input('please enter search terms separated commas:  ').split(',') parse_files(search) 

this line wrong:

    newname=join(filename, '0_parsed_log_file.txt') 

use:

    newname= "".join([filename, '0_parsed_log_file.txt']) 

join string method requires list of strings joined


Comments