0

When trying to monitor a logfile for new lines with python I stumbled upon an issue.

What I try to do:

while True:
    file = open("log.txt", "r")
    file.seek(0,2)
    where = file.tell()
    line = file.readline()

    if not line:
        time.sleep(1)
        file.seek(where)
    else:
        *do stuff with line*

The line returned is always empty. I see why this is:

file.seek(0,2)

will always move the cursor to the end of the file and therefore there is no line after the cursor. But to watch the logfile for only new lines I will need to do this.

Can someone point me in the right direction on how to approach something like this.

Franz
  • 235
  • 3
  • 14

0 Answers0