首页 > 编程语言 > 详细

Python: yield, python 实现tail -f

时间:2017-12-21 19:10:32      阅读:227      评论:0      收藏:0      [点我收藏+]

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

原文:http://www.cnblogs.com/baxianhua/p/8081821.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!