Read from a log file as it’s being written using python

Take a look at this PDF starting at page 38, ~slide I-77 and you’ll find all the info you need. Of course the rest of the slides are amazing, too, but those specifically deal with your issue:

import time
def follow(thefile):
    thefile.seek(0,2) # Go to the end of the file
    while True:
        line = thefile.readline()
        if not line:
            time.sleep(0.1) # Sleep briefly
            continue
        yield line

Leave a Comment