I am trying to read a syslog repeatedly and only start from the point I left off last read. I am trying to save the location of tell() is a sperate file and reload to seek before every read.
lf = open("location.file", 'r')
s = lf.readline()
last_pos = int(s.strip())
lf.close()
sl = open("/var/log/messages", 'r')
sl.seek(last_pos)
for line in sl.readlines():
# This should be the starting point from the last read
last_loc = sl.tell()
lf = open("location.file", "w+")
lf.write(last_loc)
lf.close()
Write
str(last_loc)instead oflast_loc.The rest are probably optional.
winstead ofw+for writing the location./var/log/messageswhen you’re done with it.withto automatially close files.stripif you’re just writing the value.readinstead ofreadlineonlf.You can iterate over the file itself, rather than using
readlines, forsl.