python代码的tail-f功能。跟踪文件追加和截断。
tailf的Python项目详细描述
尾翼
tail -f
python代码的功能。跟踪文件追加和
截断。
支持python3.6+。
示例
pip install tailf==0.2.5
importtailfimporttimewithtailf.Tail(filename)astail:whileTrue:foreventintail:ifisinstance(event,bytes):print(event.decode("utf-8"),end='')elifeventistailf.Truncated:print("File was truncated")else:assertFalse,"unreachable"# currently. more events may be introduced latertime.sleep(0.01)# save CPU cycles
# this example requires python3.7importasyncioimporttailfasyncdefmain():withtailf.Tail(filename)astail:whileTrue:event=awaittail.wait_event()ifisinstance(event,bytes):print(event.decode("utf-8"),end='')elifeventistailf.Truncated:print("File was truncated")else:assertFalse,"unreachable"# currently. more events may be introduced laterasyncio.run(main())
限制
截断检测通常是不可靠的。它主要由 文件大小减小,这有时可能不可靠。当一个文件 变大很少被截断,这就足够了。
异步跟踪在计时器事件(当前为0.01秒)时完成。 inotify支持可以在linux上解决这个问题。请随意提出其他建议 解决。