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