最近在做一个小工具,生产者不断的往一个txt文件写入数据,需要解析增量写入的数据。
代码实现如下
def read_file(filename): global location location = 0 with open(filename) as fd: while True: cur_location = fd.tell()#记录文件当前位置 if cur_location == location: #如果两者相等,说明没有新增文件 continue else: data = fd.seek(location).read()#读取增量内容 location = cur_location#移动指针到当前位置