1. 程式人生 > >Python: yield, python 實現tail -f

Python: yield, python 實現tail -f

user 運行 clas users reat for cmd bsp generator

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