Files
practical-python/Solutions/7_11/follow.py
David Beazley a83a9cf064 Many edits
2020-05-28 17:41:59 -05:00

18 lines
421 B
Python

# follow.py
import time
import os
def follow(filename):
'''
Generator that produces a sequence of lines being written at the end of a file.
'''
with open(filename,'r') as f:
f.seek(0,os.SEEK_END)
while True:
line = f.readline()
if line != '':
yield line
else:
time.sleep(0.1) # Sleep briefly to avoid busy wait