Bird
Raised Fist0

You must count how many lines in a huge log file contain the word 'FAIL' without loading the entire file into memory. Which approach is best?

hard🚀 Application Q8 of Q15
Python - File Reading and Writing Strategies

You must count how many lines in a huge log file contain the word 'FAIL' without loading the entire file into memory. Which approach is best?

ARead the entire file with read() and use count() on the string
BOpen the file and iterate line by line, incrementing a counter when 'FAIL' is in the line
CUse readlines() to get all lines and filter with a list comprehension
DLoad the file into a pandas DataFrame and filter rows containing 'FAIL'
Step-by-Step Solution
Solution:
  1. Step 1: Open file and iterate line by line

    This reads one line at a time, keeping memory usage low.
  2. Step 2: Check if 'FAIL' is in each line and count

    Increment a counter only when the condition matches.
  3. Step 3: Avoid loading entire file

    Methods like read() or readlines() load the whole file, which is inefficient for large files.
  4. Final Answer:

    Open the file and iterate line by line, incrementing a counter when 'FAIL' is in the line -> Option B
  5. Quick Check:

    Line-by-line iteration with condition check [OK]
Quick Trick: Iterate lines, count matches without full load [OK]
Common Mistakes:
MISTAKES
  • Using read() loads entire file into memory
  • Using readlines() creates large list in memory
  • Using pandas unnecessarily increases memory use

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes