I've got several hundred log files that I need to parse searching for text strings. What I would like to be able to do is run a Python script to open every file in the current folder, parse it and record the results in a new file with the original_name_parsed_log_file.txt. I had the script working on a single file but now I'm having some issues doing all files in the directory.
Below is what I have so far but it's not working atm. Disregard the first def... I was playing around with changing font colors.
import os
import string
from 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):
for filename in os.listdir(os.getcwd()):
newname=join(filename, '0_Parsed_Log_File.txt')
with open(filename) as read:
read.seek(0)
# Search line for values if found append line with spaces replaced by tabs to new file.
with open(newname, 'ab') as write:
for line in read:
for 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 by commas: ').split(',')
parse_files(search)
This line is wrong:
newname=join(filename, '0_Parsed_Log_File.txt')
use:
newname= "".join([filename, '0_Parsed_Log_File.txt'])
join
is a string method which requires a list of strings to be joined
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.