📜  使用生成器逐行读取文件 - Python 代码示例

📅  最后修改于: 2022-03-11 14:47:19.302000             🧑  作者: Mango

代码示例1
def read_file(path, block_size=1024): 
    with open(path, 'rb') as f: 
        while True: 
            piece = f.read(block_size) 
            if piece: 
                yield piece 
            else: 
                return

for piece in read_file(path):
    process_piece(piece)