How can I tail a log file in Python?

前端 未结 12 2224
故里飘歌
故里飘歌 2020-11-22 10:50

I\'d like to make the output of tail -F or something similar available to me in Python without blocking or locking. I\'ve found some really old code to do that here, but I\'

12条回答
  •  悲哀的现实
    2020-11-22 11:17

    Python is "batteries included" - it has a nice solution for it: https://pypi.python.org/pypi/pygtail

    Reads log file lines that have not been read. Remembers where it finished last time, and continues from there.

    import sys
    from pygtail import Pygtail
    
    for line in Pygtail("some.log"):
        sys.stdout.write(line)
    

提交回复
热议问题