iter_text_lines streaming for big files
Python
"""Stream text files line by line so huge inputs never load into memory."""
from collections.abc import Iterator
from pathlib import Path
def iter_text_lines(
path: str | Path,
*,
encoding: str = "utf-8",
errors: str = "strict",
skip_blank: bool = True,
) -> Iterator[str]:
"""Yield lines lazily, newline stripped, blank ones optional."""
with open(path, "r", encoding=encoding, errors=errors) as handle:
for raw in handle:
line = raw.rstrip("\n").rstrip("\r")
if skip_blank and not line.strip():
continue
yield line
if __name__ == "__main__":
for number, line in enumerate(iter_text_lines(__file__), start=1):
print(f"{number:03d}: {line[:60]}")