Hi guys,
I was wondering if anybody has any advice about parsing log files with python.
I want to parse a bunch of logs and accumulate counts for various strings / conditions in dicts.
My first task is to get lines that match the current date and are in the previous hour.
The entire day is logged to one file and will be millions of lines long so running over the files is taking a very long time.
Is there a faster way than I have shown below to check if the line contains the time?
#!/usr/bin/python -tt
import os
import glob
import sys
path = '/the/files/to/parse/'
for infile in glob.glob( os.path.join(path, 'server*/information.2011-03-24.log') ):
print infile
input_file = open(infile, 'r')
output_file = open ('/tmp/my_test.tmp', 'a')
for line in input_file:
if "24/Mar/2011:09" in line:
output_file.write(line)
input_file.close()
output_file.close()