File system interaction basics in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When working with files, it's important to know how the time to read or write changes as files get bigger.
We want to see how the program's work grows when the file size grows.
Analyze the time complexity of the following code snippet.
with open('example.txt', 'r') as file:
content = file.read()
for line in content.splitlines():
print(line)
This code reads a whole file, splits it into lines, and prints each line.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each line in the file content.
- How many times: Once for every line in the file, which depends on file size.
As the file gets bigger, the number of lines grows, so the loop runs more times.
| Input Size (n lines) | Approx. Operations |
|---|---|
| 10 | About 10 print operations |
| 100 | About 100 print operations |
| 1000 | About 1000 print operations |
Pattern observation: The work grows directly with the number of lines in the file.
Time Complexity: O(n)
This means the time to run grows in a straight line with the number of lines in the file.
[X] Wrong: "Reading a file always takes the same time no matter its size."
[OK] Correct: Bigger files have more data, so reading and processing them takes more time.
Understanding how file size affects program speed helps you write better code and answer questions clearly in interviews.
"What if we read the file line by line instead of all at once? How would the time complexity change?"
Practice
open() in Python?Solution
Step 1: Understand file modes in Python
The mode 'r' stands for reading the file only, meaning you can read data but not change it.Step 2: Compare with other modes
Modes like 'w' are for writing (which overwrites), and 'a' is for appending. 'r' does not allow writing.Final Answer:
Open the file for reading only -> Option AQuick Check:
Mode 'r' = read only [OK]
- Confusing 'r' with 'w' or 'a'
- Thinking 'r' creates a new file
- Trying to write to a file opened with 'r'
Solution
Step 1: Identify the mode for writing
The mode 'w' opens a file for writing and creates it if it doesn't exist or overwrites if it does.Step 2: Check syntax correctness
open('data.txt', 'w') is the correct syntax. 'r' is for reading, 'rw' is invalid, 'a+' is for appending and reading.Final Answer:
open('data.txt', 'w') -> Option BQuick Check:
Write mode = 'w' [OK]
- Using 'r' when intending to write
- Using invalid mode 'rw'
- Confusing 'a+' with 'w'
with open('test.txt', 'w') as f:
f.write('Hello')
with open('test.txt', 'a') as f:
f.write(' World')
with open('test.txt', 'r') as f:
print(f.read())Solution
Step 1: Write 'Hello' to the file
The first block opens 'test.txt' in write mode, which creates or clears the file, then writes 'Hello'.Step 2: Append ' World' to the file
The second block opens the file in append mode and adds ' World' after 'Hello'.Step 3: Read and print the file content
The last block reads the full content, which is 'Hello World', and prints it.Final Answer:
Hello World -> Option CQuick Check:
Write + append = 'Hello World' [OK]
- Expecting append to overwrite
- Not closing files before reading
- Confusing write and append modes
f = open('log.txt', 'r')
print(f.read())
f.write('New entry')
f.close()Solution
Step 1: Check file mode and operations
The file is opened with mode 'r' which allows reading only.Step 2: Identify invalid operation
Calling f.write() on a file opened in read mode causes an error because writing is not allowed.Final Answer:
File is opened in read mode but write is attempted -> Option AQuick Check:
Write not allowed in 'r' mode [OK]
- Trying to write without 'w' or 'a' mode
- Forgetting to close files
- Assuming 'r' mode allows writing
Solution
Step 1: Use 'with' and read line by line
with open('log.txt', 'r') as f: for line in f: if 'error' in line: print(line.strip()) uses 'with' to open the file safely and iterates line by line, which is memory efficient.Step 2: Check condition and print matching lines
It checks if 'error' is in each line and prints the line without extra spaces using strip().Final Answer:
with open('log.txt', 'r') as f: for line in f: if 'error' in line: print(line.strip()) -> Option DQuick Check:
Use 'with' + for line in file + condition [OK]
- Opening file in 'w' mode when reading
- Comparing whole line to 'error' instead of substring
- Not closing file properly
- Using split incorrectly for line filtering
