Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What Python function is used to open a file for reading or writing?
The open() function is used to open a file. You can specify the mode like 'r' for reading or 'w' for writing.
Click to reveal answer
beginner
What does the mode 'r' mean when opening a file?
Mode 'r' means open the file for reading only. The file must exist or Python will give an error.
Click to reveal answer
beginner
How do you properly close a file after opening it?
You call the close() method on the file object, like file.close(). This frees system resources.
Click to reveal answer
intermediate
What is the advantage of using a with statement when working with files?
Using with automatically closes the file when the block ends, even if errors happen. It keeps code safe and clean.
Click to reveal answer
beginner
How do you write text to a file in Python?
Open the file in write mode ('w'), then use file.write('your text') to add text. Remember to close the file or use with.
Click to reveal answer
Which mode opens a file for appending data without deleting existing content?
A'w'
B'r'
C'x'
D'a'
✗ Incorrect
Mode 'a' opens the file for appending. It adds new data at the end without removing existing content.
What happens if you try to open a non-existent file in 'r' mode?
APython creates a new empty file
BPython raises an error
CPython opens the file in write mode
DPython ignores the request
✗ Incorrect
Opening a file in 'r' mode requires the file to exist. If it doesn't, Python raises a FileNotFoundError.
Which method reads the entire content of a file as a string?
Aread()
Bwrite()
Cclose()
Dopen()
✗ Incorrect
The read() method reads all the content from the file and returns it as a string.
Why is it better to use 'with open(...) as file:' instead of just open()?
AIt automatically closes the file after use
BIt makes the file read-only
CIt speeds up file reading
DIt encrypts the file content
✗ Incorrect
Using 'with' ensures the file is closed automatically, even if errors occur, preventing resource leaks.
What does the 'w' mode do when opening a file?
AReads the file
BAppends to the file
CWrites to the file, erasing existing content
DCreates a new file only if it doesn't exist
✗ Incorrect
Mode 'w' opens the file for writing and erases any existing content before writing.
Explain how to safely read the contents of a text file in Python.
Think about how to open, read, and close a file safely.
You got /4 concepts.
Describe the difference between 'w' and 'a' modes when opening a file.
Consider what happens to existing content in each mode.
You got /4 concepts.
Practice
(1/5)
1. What does the mode 'r' mean when opening a file with open() in Python?
easy
A. Open the file for reading only
B. Open the file for writing only
C. Open the file for appending data
D. Create a new file or overwrite existing
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 A
Quick Check:
Mode 'r' = read only [OK]
Hint: Remember 'r' means read, 'w' means write, 'a' means append [OK]
Common Mistakes:
Confusing 'r' with 'w' or 'a'
Thinking 'r' creates a new file
Trying to write to a file opened with 'r'
2. Which of the following is the correct syntax to open a file named 'data.txt' for writing in Python?
easy
A. open('data.txt', 'r')
B. open('data.txt', 'w')
C. open('data.txt', 'rw')
D. open('data.txt', 'a+')
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 B
Quick Check:
Write mode = 'w' [OK]
Hint: Use 'w' to write or overwrite files [OK]
Common Mistakes:
Using 'r' when intending to write
Using invalid mode 'rw'
Confusing 'a+' with 'w'
3. What will be the output of this code?
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())
medium
A. Error: file not found
B. Hello
C. Hello World
D. World
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 C
Quick Check:
Write + append = 'Hello World' [OK]
Hint: Write clears file, append adds to end [OK]
Common Mistakes:
Expecting append to overwrite
Not closing files before reading
Confusing write and append modes
4. What is wrong with this code snippet?
f = open('log.txt', 'r')
print(f.read())
f.write('New entry')
f.close()
medium
A. File is opened in read mode but write is attempted
B. File is not closed properly
C. Missing mode argument in open()
D. File path is incorrect
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 A
Quick Check:
Write not allowed in 'r' mode [OK]
Hint: Don't write to files opened with 'r' mode [OK]
Common Mistakes:
Trying to write without 'w' or 'a' mode
Forgetting to close files
Assuming 'r' mode allows writing
5. You want to read a file line by line and print only lines that contain the word 'error'. Which is the best way to do this in Python?
hard
A. open('log.txt', 'r').read().split('error')
B. f = open('log.txt', 'r')
lines = f.readlines()
for line in lines:
if line == 'error':
print(line)
f.close()
C. with open('log.txt', 'w') as f:
for line in f:
if 'error' in line:
print(line)
D. with open('log.txt', 'r') as f:
for line in f:
if 'error' in line:
print(line.strip())
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 D
Quick Check:
Use 'with' + for line in file + condition [OK]
Hint: Use 'with' and loop lines to filter content [OK]
Common Mistakes:
Opening file in 'w' mode when reading
Comparing whole line to 'error' instead of substring