python - Extracting new errors from log file and emailing result -
the following python 2.5 script works beginner wonder if there blatant mistakes or perhaps better way trying achieve?
the aim open current day's log - '/library/application support/perceptive automation/indigo 5/logs/' , extract lines have word error , email new errors. after error lines extracted number of lines counted (prenumlines) in tmp.txt. extracted lines written tmp.txt file , lines counted again (postnumlines). line numbers greater prenumlines printed 'thebody' , emailed.
from datetime import date import linecache filedate = str(date.today()) thebody = [] tmpfile = open('/library/application support/perceptive automation/indigo 5/logs/tmp.txt') prenumlines = sum(1 line in tmpfile) log= open( '/library/application support/perceptive automation/indigo 5/logs/' + filedate + ' events.txt', 'r' ) tmpfile = open('/library/application support/perceptive automation/indigo 5/logs/tmp.txt', 'w') line in log: if 'error' in line: tmpfile.write(line ) log.close() tmpfile.close() postnumlines = sum(1 line in open('/library/application support/perceptive automation/indigo 5/logs/tmp.txt')) linenum = prenumlines while linenum < postnumlines: thebody.append(linecache.getline( '/library/application support/perceptive automation/indigo 5/logs/tmp.txt', linenum + 1) ) linenum = linenum + 1 tmpfile.close() thebody = "".join(thebody) #thebody body of email sent next #print thebody
there changes you, speaking experience, should consider making (i use must, still recommendation), , others more personal style (where use should).
on meta level: question tagged python-25 why did not attention, must use python has many more followers on so.
you must not use tab , space characters in file. source indented incorrectly here on because of that. line: tmpfile.write(line) should indented 1 level under if statement above it.
you must not use same string filename 4 times, replace variable. , create variable common base directory using os.path.join()
you should consider following pep8 style guide python.
your program not run if try first time around, 'tmp.txt' not exist. fail gracefully prenumlines of 0 (however not using @ anymore, see below):
try: # open file # count lines except: prenumlines = 0 i wonder state code works, overwrite tmp.txt , write error lines from last log in there. moment assign tmpfile again, first open reading closed. close tmpfile opened writing twice (for python not throw error). if yesterdays log had 1 error , todays three, email show 2 lines. append file use open(filename, 'a')
you should consider using with statement (new in python 2.5) when reading/writing files, way dispense (erroneous .close() statements).
you should consider creating thebody string , appending lines. using list first , join them might faster, script running once day.
there no need count lines , append error lines , reread them , store them in thebody, should dispense first , third part , in 1 go:
from __future__ import with_statement import os datetime import date basedir = '/library/application support/perceptive automation/indigo 5/logs' tmpfilename = os.path.join(basedir, 'tmp.txt') filedate = str(date.today()) eventfilename = os.path.join(basedir, filedate + ' events.txt') thebody = '' open(tmpfilename, 'a') tmpfile: open( eventfilename, 'r' ) log: line in log: if 'error' in line: tmpfile.write(line) thebody += line # thebody body of email sent next print thebody
Comments
Post a Comment