Reading files line by line in Python - Time & Space Complexity
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?"