mbsenturk
12/5/2017 - 8:17 PM

Read a file in chunks without loading the whole file into memory.

Read a file in chunks without loading the whole file into memory.

def read_file_chunks(chunk_size, file_obj):
    i = 0
    lines = []
    for l in file_obj:
        lines.append(l.rstrip())
        i += 1
        if i%chunk_size == 0:
            yield lines
            lines = []
    yield lines