← Back to Snippets
PythonApr 2026
Chunked file reading
Read large files in fixed-size chunks to avoid loading the entire file into memory.
snippet
def read_in_chunks(filepath, chunk_size=1024 * 64):
"""Yield 64 KB chunks from a binary file."""
with open(filepath, 'rb') as f:
while chunk := f.read(chunk_size):
yield chunk
# For text files — Python streams lines automatically
with open('large.log', 'r') as f:
for line in f:
process(line.strip())
# For binary files — use the generator
for chunk in read_in_chunks('large.bin'):
process_chunk(chunk)File I/OMemoryGenerator