Python 读取几百 GB 的文件,不爆内存

def myreadlines(f, newline):
    buf = ''
    while True:
        while newline in buf:
            pos = buf.index(newline)
            yield buf[:pos]
            buf = buf[pos + len(newline):]
        chunk = f.read(4096) # 每次读取的大小

        if not chunk:
            yield buf
            break
        buf += chunk
        

if __name__ == '__main__':
    # 文件中的分隔符
    flite = r"\n"
    with open("contain.txt") as f:
        for line in myreadlines(f, flite):
            print(line)
posted @ 2020-04-03 19:40  Nefarious  阅读(365)  评论(0)    收藏  举报