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

posted @ 2017-12-21 17:59  筱筱的春天  阅读(863)  评论(0编辑  收藏  举报