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."
Related
i tried to make auto check if file is modified then print and send to discord using webhook, but it print the last line and before the last line. i just want to print the last line. here is the code
def readfile():
with open("test.txt", "r") as t:
read = t.readlines()
return read
before = readfile()
while True:
after = readfile()
if before != after:
for line in after:
if line not in before:
print(line)
sendembed()
before = after
im new to python, anyone can help me?
given that the first part is answered with the link: How do I watch a file for changes?
This will answer the second part, which the question was close on.
def readfile():
with open(`\test.txt`, "r") as t:
read = t.readlines()
return read
x = readfile()
# this is the last line
print( x[-1] )
The reason for this is that readlines() creates a list and the user can access the last element of the list.
I am trying to append the lines of a file (.py file) to a list as a way to count the lines and figure out which lines are code using a conditional statement. The problem is that my function that I created is reading the 'file name' instead of the lines of the file itself. Where did I go wrong with this. I am surprised I got this far...it works, but not for the right reason.
from tkinter.filedialog import askopenfilename
import time
def getFileName():
sourceCode = askopenfilename() # Opens dialog box to locate and select file
return sourceCode
def scLines():
scList = []
sourceCode = getFileName()
for line in sourceCode:
if line[0] != "#" or line != "":
scList.append(line)
return scList
def countscLine():
lineCount = len(scLines())
return lineCount
def fCount():
fList = []
sourceCode = getFileName()
for line in sourceCode:
if line[0:3] == 'def ':
fAmout.append(line)
lineCount = len(fList)
return fList
# Get file name from user
def main():
print("Select the file to be analyzed")
time.sleep(5) # Waits 5 seconds before initiating function
sourceCode = getFileName()
print("The file is", sourceCode)
print("The source code has ", countscLine(), "lines of code, and", fCount(), "functions.")
print(scLines())
print("")
main()
The problem is for line in sourceCode:. You need to actually open the file.
with open(sourceCode) as f:
for line in f:
if line[0] != "#" or line != "":
scList.append(line)
I'd recommend renaming some of your variables to be more clear as to what they actually do. For example, I would call sourceCode sourceCodefn or similar to indicate that it is a file name.
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.
so im writing a login system with python and i would like to know if i can search a text document for the username you put in then have it output the line it was found on and search a password document. if it matches the password that you put in with the string on that line then it prints that you logged in. any and all help is appreciated.in my previous code i have it search line one and if it doesnt find the string it adds one to line then repeats till it finds it. then it checks the password file at the same line
def checkuser(user,line): # scan the username file for the username
ulines = u.readlines(line)
if user != ulines:
line = line + 1
checkuser(user)
elif ulines == user:
password(user)
Pythonic way for your answer
f = open(filename)
line_no = [num for num,line in enumerate(f) if 'searchstring' in line][0]
print line_no+1
fucntion to get the line number. You can use this how you want
def getLineNumber(fileName, searchString):
with open(fileName) as f:
for i,line in enumerate(f, start=1):
if searchString in line:
return i
raise Exception('string not found')
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.