reading log files in Python and outputing specific text - python

I have a piece of code that reads the last line of a log file as the log is being written to. I want to print errors which occur in the logs, basically start printing when line.startswith('Error') and finish printing when line.startwith('End of Error'). My code is below, Could anybody help me with this please?
log = 'C:\mylog.log'
file = open(log, 'r')
res = os.stat(log)
size = res[6]
file.seek(size)
while 1:
where = file.tell()
line = file.readline()
if not line:
time.sleep(1)
file.seek(where)
else:
if line.startswith('Error'):
#print lines until you come to 'End of Error'

Initialize a flag before the loop:
in_error = False
Then switch it on and off as needed:
if line.startswith('Error'):
in_error = True
elif line.startswith('End of Error'):
print(line)
in_error = False
if in_error:
print(line)

It may be easier to use the subprocess module to simply run tail -F (capital F, available on GNU platforms) and process the output.

Related

conditions on python when i check hard bounces email

i write Python script to verify hard bounces
from validate_email import validate_email
with open("test.txt") as fp:
line = fp.readline()
cnt = 1
while line:
line = fp.readline()
print ('this email :' + str(line) +'status : ' + str((validate_email(line,verify=True))))
stt=str(validate_email(line,verify=True))
email=str(line)
print ("-----------------")
cnt += 1
if stt == "True":
file=open("clean.txt",'w+')
file.write(email)
if stt == "None":
file=open("checkagain.txt",'w+')
file.write(email)
if stt == "False":
file=open("bounces.txt",'w+')
file.write(email)
for False condition it create the file but no emails inside even if am sure that i have bounces emails
You need to close the file to reflect your changes in file, put:
file.close()
at the end
you should instead be using:
with open('bounces.txt', 'a') as file:
# your file operations
that way you wont have to close the file
Your script contains a number of errors.
Each input line contains a trailing newline.
Opening the same file for writing multiple times is hideously inefficient. Failing to close the files is what caused them to end up empty. Reopening without closing might end up discarding things you've written on some platforms.
Several operations are repeated, some merely introducing inefficiencies, others outright errors.
Here is a refactoring, with inlined comments on the changes.
from validate_email import validate_email
# Open output files just once, too
with open("test.txt") as fp, \
open('clean.txt', 'w') as clean, \
open('checkagain.txt', 'w') as check, \
open('bounces.txt', 'w') as bounces:
# Enumerate to keep track of line number
for i, line in enumerate(fp, 1):
# Remove trailing newline
email = line.rstrip()
# Only validate once; don't coerce to string
stt = validate_email(email, verify=True)
# No need for str()
print ('this email:' + email +'status: ' + stt)
# Really tempted to remove this, too...
print ("-----------------")
# Don't compare to string
if stt == True:
clean.write(line)
elif stt == None:
check.write(line)
elif stt == False:
bounces.write(line)
You are not using the line number for anything, but I left it in to show how it's usually done.

MD5 decrypt script

__author__ = 'Zane'
import hashlib
import sys
if (len(sys.argv)!=2 ) or (len(sys.argv[1])!= 32):
print("[---] md5cracker.py & hash")
sys.exit(1)
crackedmd5 = sys.argv[1]
# open a file and read its contents
f = open('file.txt')
lines = f.readline()
f.close()
for line in lines:
cleanline = line.rstrip()
hashobject = hashlib.md5(cleanline)
if (hashobject==crackedmd5):
print('Plain text password for ' + crackedmd5 + "is " + hashobject + '\n')
I get no error with exit code 1 and i do not know where i get it wrong
Your program exits with status code one because you told it so (roughly on line 8):
sys.exit(1)
Pythons code structure is based on indent of lines. For now your whole code is part of the if (len(sys.argv)!=2 ) or (len(sys.argv[1])!= 32): condition.
You need to unindent all lines with one tab starting from crackedmd5 = sys.argv[1]
EDIT
You also used lines = f.readline() which will read only one line and so for line in lines will iterate over every single char in that line and not over multiple lines. You need to use lines = f.readlines() instead.

Python Search irc log file for last line with phrase COMPLETE:

I'm currently using pyinotify to monitor an irssi (irc client for linux command line) log file, and this log file is written to by irssi using the IN_MODIFY mask from pyinotify, it reads the file just fine, and is told every time when its changed, but I'm trying to find a specific phrase in a line, in this line the phrase is:
COMPLETE:
Now i have code that checks the last line to see if its there, but irssi writes to the logfile in chunks sometimes because things come in all at once due to it being a bot readout. This code works fine, what I'm having problems with is finding the last line closest to the bottom with the COMPLETE: phrase in it.
I know I'm probably doing it wrong but the way I've come up with(and it doesn't work, so i am doing it wrong) is to save the last COMPLETE: line and search for that line and for loop enumerate over the file with an offset of the line number the last line is found at and process each complete line after that. Below is the code, if anyone has any ideas, or perhaps a better way of doing this, it would be greatly appreciated!
def getlastline(self):
logfile = "/home/user/irclogs/kta/#kta-all.log"
with open(logfile, "rb") as f:
if self.lastline != "":
#This part doesn't work.
thenum = 0
for num, line in enumerate(f, 1):
if line == self.lastline:
thenum = num
if thenum == 0:
return
for i, ln in enumerate(f, thenum):
if ln.find("COMPLETE:") > 0:
self.setlast(ln)
self.extractdata(ln)
return
else:
#This part here works
first = f.readline()
f.seek(-2, 2)
while f.read(1) != "\n":
f.seek(-2, 1)
last = f.readline()
if last.find("COMPLETE:") > 0:
self.setlast(last)
self.extractdata(last)
else:
#This part doesn't work
for line in reversed(f.readlines()):
if line.find("COMPLETE:") > 0:
self.setlast(line)
return
Edit:
self.extractdata is a function to regex out certain parts of the line i'm getting, the line is sent to self.extractdata from the getlastline function, and self.setlast merely sets self.lastline and print's something for testing.
def __init__(self):
self.lastline = ""
def setlast(self, line):
self.lastline = line
print "Next-Newest complete line found."

How to convert this sed command to Python script?

I have one old shell script which include sed command as below.
The source data($Tmp) is a HTML table.
sed '/<table border/,/table>/d' $Tmp > $Out
Can someone help me to convert this command to Python script?
I really can't figure out how to do that with regular expression.
Many thanks..
Here's a simple implementation.
Briefly, it opens the file, iterates line by line and prints each line to the output. If it matches "<table border", delete flag set to True and following lines aren't printed to the output until it matches "table>".
import sys
f = open(sys.argv[1])
delete = False
for line in f:
if delete == False:
if "<table border" in line:
delete = True
if delete == False:
print line,
if delete == True:
if "table>" in line:
delete = False
The script copys all lines from the input file to the output file, unless it finds a line containing <table border, then it deletes all lines until it finds /table> and continues writing all further lines.
So one possibility would be:
with open('in') as inf, open('out', 'w') as outf:
while True:
line = inf.readline()
if '<table border' in line:
while True:
line = inf.readline()
if not line or '/table>' in line:
line = inf.readline()
break
if not line:
break
outf.write(line)
import sys
with open(sys.argv[1]) as f:
for line in f:
if '<table border' in line:
for line in f:
if 'table>' in line:
break
else:
sys.stdout.write(line)

Python generates an IO error while interleaving open/close/readline/write on the same file

I'm learning Python-this gives me an IO error-
f = open('money.txt')
while True:
currentmoney = float(f.readline())
print(currentmoney, end='')
if currentmoney >= 0:
howmuch = (float(input('How much did you put in or take out?:')))
now = currentmoney + howmuch
print(now)
str(now)
f.close()
f = open('money.txt', 'w')
f.write(str(now))
f.close()
Thanks!
The while True is going to loop forever unless you break it with break.
The I/O error is probably because when you have run through the loop once the last thing you do is f.close(), which closes the file. When execution continues with the loop in the line currentmoney = float(f.readline()): f will be a closed filehandle that you can't read from.
well theres a couple of things...
you open(money.txt) outside the while loop but you close it after the first iteration...
(technically you close, reopen & close again)
Put when the loop comes round the second time, f will be closed and f.readLine() will most likely fail
You close your file only if the IF condition is satisfied, otherwise you attempt to reopen it after the IF block. Depending on the result you want to achieve you will want to either remove the f.close call, or add an ELSE branch and remove the second f.open call. Anyway let me warn you that the str(now) in your IF block is just deprecated as you're not saving the result of that call anywhere.
You'll get an IO Error on your first line if money.txt doesn't exist.
Can I piggyback a question? The following has puzzled me for some time. I always get an IOError from these 'open()' statements, so I've stopped checking for the error. (Don't like to do that!) What's wrong with my code? The 'if IOError:' test shown in comments was originally right after the statement with 'open()'.
if __name__ == '__main__':
#get name of input file and open() infobj
infname = sys.argv[1]
print 'infname is: %s' % (sys.argv[1])
infobj = open( infname, 'rU' )
print 'infobj is: %s' % infobj
# 'if IOError:' always evals to True!?!
# if IOError:
# print 'IOError opening file tmp with mode rU.'
# sys.exit( 1)
#get name of output file and open() outfobj
outfname = sys.argv[2]
print 'outfname is: %s' % (sys.argv[2])
outfobj = open( outfname, 'w' )
print 'outfobj is: %s' % outfobj
# if IOError:
# print 'IOError opening file otmp with mode w.'
# sys.exit( 2)

Categories