Python: yield, python 实现tail -f

Posted 筱筱的春天

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python: yield, python 实现tail -f相关的知识,希望对你有一定的参考价值。

def CreateGenerator(file):

    with open(file,‘r‘) as t:

        t.seek(0,2)

        while True:

    line=t.readline()

    if not line:

      time.sleep(0.1)

      continue

    yield line

g=CreateGenetor(r‘C:\Users\Runlog.log)

for i in g:

  print(i)

 

script:

可以在cmd里面运行C:\python\Python1\Python1\Python1.py  C:\Users\Runlog.log,可以不加路径,更新文件保存后便能获取到新增加的log

以上是关于Python: yield, python 实现tail -f的主要内容,如果未能解决你的问题,请参考以下文章

可以在没有 await 或 yield 的情况下实现 Python 协程吗?

python生成器中yield和send分析

00.用 yield 实现 Python 协程

Python生成器实现及yield关键字

Python yield

python基础学习日志day8-实现进度条功能,for和yield实现