Python Generators explained simply: How to read GB-sized files without crashing your system

Posted by Possible-Steak2633@reddit | Python | View on Reddit | 2 comments

I was working with a server log file that was several GBs in size. Used a normal function with readlines() — system froze completely.

That's when I properly understood why Python Generators exist. Sharing what I learned in case it helps others here.

**The Problem:**

When you read a large file using a regular function, Python loads the ENTIRE content into RAM. If the file is large enough, your program crashes.

```python

# This WILL crash on large files

def read_file(path):

with open(path, 'r') as f:

return f.readlines() # All lines loaded into memory at once

```

**The Solution — Generators with yield:**

Generators use the `yield` keyword instead of `return`. They produce one value at a time and pause execution until the next value is requested. Think of it like a water tap — drop by drop, not a flood.

```python

def read_large_file(file_path):

"""Yields one line at a time"""

with open(file_path, 'r') as f:

for line in f:

yield line.strip()

# Usage: Filter only error lines from server log

for line in read_large_file('server.log'):

if 'ERROR' in line:

print(line)

```

**Key differences:**

- `return` → sends ALL data at once, function ends

- `yield` → sends ONE value, function pauses, remembers its state

- Generators implement `__iter__()` and `__next__()` automatically

- You can use `next()` to manually get the next value

**When to use Generators:**

- Reading large files (logs, CSVs, datasets)

- Streaming data processing

- Infinite sequences

- Any situation where loading everything into memory isn't feasible

**Why it matters for interviews:**

This is a very common Python interview question. Interviewers want to see if you understand lazy evaluation and memory-efficient programming.

I also made a detailed video walkthrough with a live VS Code demo if anyone's interested: https://youtu.be/Rl_F47kD_Z8 (it's in Hindi/Hinglish but the code is universal)

Happy to answer any questions!