Build a lazy generator to read file lines in Python
Create a generator function that yields file lines one at a time, avoiding loading the entire file into memory, and demonstrate its lazy processing.
Python code
22 linesdef lazy_lines(filepath):
"""Yield lines from a file one at a time without loading the whole file into memory."""
with open(filepath, 'r', encoding='utf-8') as file:
for line in file:
yield line.rstrip('\n')
if __name__ == "__main__":
# Create a sample file to demonstrate
sample_content = "first line\nsecond line\nthird line\nfourth line"
with open("sample.txt", "w") as f:
f.write(sample_content)
# Use the generator lazily
line_generator = lazy_lines("sample.txt")
# Process lines one by one
for idx, line in enumerate(line_generator, start=1):
print(f"Line {idx}: {line}")
# Show it's a generator object
print(f"\nType: {type(line_generator)}")
Output
Line 1: first line
Line 2: second line
Line 3: third line
Line 4: fourth line
Type: <class 'generator'>
How it works
The lazy_lines function is a generator because it uses the yield keyword. It opens the file and iterates over each line, stripping the trailing newline before yielding it. Because it's a generator, it reads one line at a time from the file, so the whole file is never loaded into memory. This is efficient for large files. The with statement ensures the file is closed properly after iteration completes.
Common mistakes
- Using `readlines()` instead of iterating directly, which loads the entire file into memory
- Forgetting to strip newline characters, leaving `\n` in the yielded lines
- Not closing the file manually when not using a context manager
Variations
- Use `yield from file` to yield lines without stripping newlines
- Use a list comprehension over the file object to create a list, but this is not lazy
Real-world use cases
- Processing huge log files line-by-line in a memory-constrained environment.
- Streaming data from a CSV file into a database without loading the entire dataset.
- Implementing a custom file reader in a pipeline that processes records incrementally.
Sponsored
More from Comprehensions & generators
- Batch Rows in Chunks with a Generator in Python easy
- Build a Generator Pipeline in Python: Filter Then Map medium
- Chunk an Iterable into Batches with a Generator in Python easy
- Convert Data in Python with Comprehensions and Generators easy
- Count Data in Python with Comprehensions and Generators easy
- Cycle an iterable forever in Python easy
Keep learning
Related tutorials and quizzes for this topic.