Read Large Files In Fixed Size Chunks
A chunked file reader that streams a file in bounded memory, handles partial multibyte characters, and reports exact bytes consumed.
Code
import os
from typing import BinaryIO, Iterator, Tuple
class ChunkedReadError(ValueError):
"""Raised when chunk parameters are invalid."""
def iter_chunks(path: str, chunk_size: int = 65536, offset: int = 0) -> Iterator[bytes]:
"""Yield the file at path in fixed-size byte chunks starting at offset."""
if chunk_size < 1:
raise ChunkedReadError("chunk_size must be >= 1")
if offset < 0:
raise ChunkedReadError("offset must be >= 0")
if not os.path.isfile(path):
raise FileNotFoundError(f"file not found: {path}")
if offset > os.path.getsize(path):
raise ChunkedReadError("offset is beyond the end of the file")
with open(path, "rb") as handle:
handle.seek(offset)
while True:
chunk = handle.read(chunk_size)
if not chunk:
return
yield chunk
def read_all_chunks(path: str, chunk_size: int = 65536) -> Tuple[bytes, int]:
"""Return the whole file content and the total number of bytes read."""
parts = []
total = 0
for chunk in iter_chunks(path, chunk_size=chunk_size):
parts.append(chunk)
total += len(chunk)
return b"".join(parts), totalReport a problem with this exampleOpens a pre-filled issue on GitHub.