Generator that produces a sequence of lines being written at the end of a file.
(filename)
| 4 | import os |
| 5 | |
| 6 | def follow(filename): |
| 7 | ''' |
| 8 | Generator that produces a sequence of lines being written at the end of a file. |
| 9 | ''' |
| 10 | with open(filename,'r') as f: |
| 11 | f.seek(0,os.SEEK_END) |
| 12 | while True: |
| 13 | line = f.readline() |
| 14 | if line != '': |
| 15 | yield line |
| 16 | else: |
| 17 | time.sleep(0.1) # Sleep briefly to avoid busy wait |