Read a large log file (10 GB) and count lines matching a pattern without running out of memory.

BeginnerHands-onPython for DevOps
Answer
count = 0
with open("/var/log/huge.log", "r", errors="replace") as f:
    for line in f:              # streams line by line
        if " 500 " in line:
            count += 1
print(count)

Key trick → Iterating the file object streams it. f.read() or readlines() would load all 10 GB into memory. errors="replace" survives stray non-UTF-8 bytes.

All Real-World Interview questions Roadmap: Interview readiness

More on Python for DevOps