Reading files line by line in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When reading a file line by line, we want to know how the time it takes grows as the file gets bigger.
We ask: How does the program's work increase when there are more lines to read?
Analyze the time complexity of the following code snippet.
with open('file.txt', 'r') as file:
for line in file:
print(line.strip())
This code opens a file and reads it one line at a time, printing each line after removing extra spaces.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each line in the file.
- How many times: Once for every line in the file.
As the number of lines grows, the program reads and processes more lines one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 line reads and prints |
| 100 | About 100 line reads and prints |
| 1000 | About 1000 line reads and prints |
Pattern observation: The work grows directly with the number of lines; double the lines, double the work.
Time Complexity: O(n)
This means the time to read the file grows in a straight line with the number of lines.
[X] Wrong: "Reading a file line by line is always very slow because it reads each character individually."
[OK] Correct: The code reads line by line, not character by character, so it processes one line at a time efficiently.
Understanding how reading files scales helps you write programs that handle big data smoothly and shows you think about efficiency.
"What if we read the whole file at once into memory instead of line by line? How would the time complexity change?"
Practice
with open('data.txt') as file:
for line in file:
print(line)Solution
Step 1: Understand the with open statement
The code opens 'data.txt' for reading and ensures it closes automatically after use.Step 2: Analyze the for loop over the file object
Looping over the file reads it line by line, printing each line including its newline character.Final Answer:
Reads and prints each line from 'data.txt' including newline characters -> Option DQuick Check:
Reading lines one by one = Reads and prints each line from 'data.txt' including newline characters [OK]
- Thinking it reads whole file at once
- Confusing reading with writing
- Assuming it creates a file
Solution
Step 1: Check the correct use of with open
with open('file.txt') as f: for line in f: print(line) correctly uses 'with open(filename) as f:' to open the file safely.Step 2: Verify the for loop syntax
with open('file.txt') as f: for line in f: print(line) uses 'for line in f:' which is the proper way to iterate lines in the file object.Final Answer:
with open('file.txt') as f:\n for line in f:\n print(line) -> Option AQuick Check:
Correct with open and for loop syntax = with open('file.txt') as f: for line in f: print(line) [OK]
- Missing 'with' keyword
- Using wrong loop syntax like 'while line in f'
- Using undefined variable 'file' instead of 'f'
with open('test.txt') as f:
for line in f:
print(line.strip())Solution
Step 1: Understand strip() effect on lines
strip() removes whitespace including newlines, so empty lines become empty strings.Step 2: Analyze printing each stripped line
Empty lines print as blank lines, but print() adds a newline, so empty lines show as blank lines.Final Answer:
Line1\nLine2\n\nLine4 -> Option AQuick Check:
strip() on empty -> '', print('') = blank line = Line1\nLine2\n\nLine4 [OK]
- Assuming strip() keeps newlines
- Confusing blank lines with extra newlines
- Ignoring print() adds newline automatically
with open('file.txt') as f:
for line in file:
print(line)Solution
Step 1: Check variable names used in the loop
The file is opened as 'f', but the loop uses 'file' which is undefined.Step 2: Confirm correct variable usage
Changing 'file' to 'f' fixes the error and allows reading lines properly.Final Answer:
Variable 'file' is undefined; should use 'f' instead -> Option CQuick Check:
Use same variable name as in with open = Variable 'file' is undefined; should use 'f' instead [OK]
- Using different variable names
- Assuming file is a keyword
- Ignoring variable scope inside with block
Solution
Step 1: Choose efficient line-by-line reading
count = 0 with open('log.txt') as f: for line in f: if 'error' in line: count += 1 print(count) uses 'with open' and iterates file line by line, which is memory efficient for large files.Step 2: Check condition for counting 'error' in line
count = 0 with open('log.txt') as f: for line in f: if 'error' in line: count += 1 print(count) correctly checks if 'error' is anywhere in the line and increments count.Final Answer:
count = 0\nwith open('log.txt') as f:\n for line in f:\n if 'error' in line:\n count += 1\nprint(count) -> Option BQuick Check:
Efficient reading + correct condition = count = 0 with open('log.txt') as f: for line in f: if 'error' in line: count += 1 print(count) [OK]
- Using readlines() for big files (memory issue)
- Forgetting to close file without with
- Checking line equality instead of substring
