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:UsersRunlog.log)

for i in g:

  print(i)

script:

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

原文地址:https://www.cnblogs.com/baxianhua/p/8081821.html