Reading file data in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When reading data from a file, it is important to understand how the time taken grows as the file size increases.
We want to know how the program's running time changes when the file has more lines or characters.
Analyze the time complexity of the following code snippet.
with open('data.txt', 'r') as file:
lines = file.readlines()
for line in lines:
print(line.strip())
This code reads all lines from a file and prints 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 in the file grows, the time to read and print each line grows in a similar way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 lines read and printed |
| 100 | About 100 lines read and printed |
| 1000 | About 1000 lines read and printed |
Pattern observation: The time grows roughly in direct proportion to the number of lines.
Time Complexity: O(n)
This means the time taken grows linearly with the number of lines in the file.
[X] Wrong: "Reading a file always takes the same time no matter how big it is."
[OK] Correct: The more lines or characters in the file, the longer it takes to read and process each one.
Understanding how file reading time grows helps you write efficient programs and explain your reasoning clearly in interviews.
"What if we read the file line by line inside the loop instead of reading all lines at once? How would the time complexity change?"
Practice
open('file.txt', 'r') command do in Python?Solution
Step 1: Understand the open() function
Theopen()function is used to open a file in a specified mode.Step 2: Recognize mode 'r'
Mode 'r' means open the file for reading only, no writing or creating.Final Answer:
It opens the file 'file.txt' for reading. -> Option CQuick Check:
open() with 'r' = open for reading [OK]
- Confusing 'r' with write mode 'w'
- Thinking it creates a new file
- Assuming it deletes the file
with?Solution
Step 1: Check the use of 'with' statement
The 'with' statement must be followed byopen(filename, mode) as variableto assign the file object.Step 2: Verify reading mode and method
Mode 'r' is for reading, andfile.read()reads all content.Final Answer:
with open('data.txt', 'r') as file: content = file.read() -> Option DQuick Check:
with + open + 'r' + read() = correct syntax [OK]
- Using 'w' mode when reading is needed
- Missing 'as file' after open()
- Not indenting inside 'with' block
with open('example.txt', 'r') as f:
lines = f.readlines()
print(lines)Solution
Step 1: Understand readlines() behavior
readlines()reads all lines into a list, each line ending with a newline character '\n' except possibly the last.Step 2: Check the file content and output
Since the file has three lines, the list will contain each line as a string with '\n' at the end except maybe the last line. Usually, text files end lines with '\n', so all lines have '\n'.Final Answer:
['apple\n', 'banana\n', 'cherry\n'] -> Option AQuick Check:
readlines() returns list of lines with '\n' [OK]
- Assuming readlines() strips '\n'
- Confusing read() output with readlines()
- Expecting a single string instead of list
file = open('notes.txt', 'r')
for line in file.read():
print(line)
file.close()Solution
Step 1: Analyze the for loop iteration
The code usesfile.read()which returns a single string of the whole file content.Step 2: Understand iteration over string vs lines
Iterating over a string loops over each character, not each line. To read line by line, usefile.readlines()or iterate directly onfile.Final Answer:
Using file.read() instead of file.readlines() or iterating directly on file -> Option BQuick Check:
read() returns string, not list of lines [OK]
- Iterating over string instead of lines
- Forgetting to close the file
- Confusing read() and readline()
Solution
Step 1: Remove whitespace and filter empty lines
Usingline.strip()removes spaces and newline characters from both ends. The conditionif line.strip()filters out empty lines.Step 2: Use list comprehension on file object
Iterating directly on the file object reads line by line efficiently. This creates a list of cleaned, non-empty lines.Final Answer:
with open('log.txt', 'r') as f: lines = [line.strip() for line in f if line.strip()] -> Option AQuick Check:
strip() + filter empty lines = clean list [OK]
- Not stripping newline characters
- Including empty lines in the list
- Using read() then splitting without filtering empty lines
