Python3之yeild监听器应用

import time


def monitor(filename):
    with open(filename, encoding='utf-8') as f:
        f.seek(0, 2)  # 从文件末尾算起
        while True:
            line = f.readline()  # 读取文件中新的文本行
            if not line:
                time.sleep(0.1)
                continue
            yield line


for line in monitor('a.txt'):
    print(line, end='')

你可能感兴趣的:(Python)