Python File Handling Basics Quiz
A 35-question quiz covering file opening, reading, writing, modes, and the importance of context managers.
Question 1
What is the primary purpose of the `open()` function in Python?
Question 2
Analyze the code below. What happens if 'data.txt' does not exist?
f = open('data.txt', 'r')Question 3
Which file mode should you use to add new content to the end of an existing file without deleting its current contents?
Question 4
Analyze the code below. What is the best practice missing from this snippet?
f = open('log.txt', 'w')
f.write('Error occurred')Question 5
What is the main advantage of using the `with open(...)` statement?
Question 6
Which method reads the *entire* remaining contents of a file as a single string?
Question 7
Analyze the code below. What will be the content of 'test.txt' after execution?
with open('test.txt', 'w') as f:
f.write('Hello')
with open('test.txt', 'w') as f:
f.write('World')Question 8
What does the `readline()` method return when it reaches the end of the file?
Question 9
Which file mode allows you to both read and write to a file?
Question 10
Analyze the code below. What is the data type of `lines`?
with open('data.txt', 'r') as f:
lines = f.readlines()Question 11
What happens if you try to write to a file opened in `'r'` mode?
Question 12
How do you ensure that data written to a file is physically saved to the disk immediately without closing the file?
Question 13
Analyze the code below. Why might the output contain double newlines?
with open('file.txt', 'r') as f:
for line in f:
print(line)Question 14
Which mode creates a new file for writing but fails if the file already exists?
Question 15
What is the default mode if you omit the mode argument in `open('file.txt')`?
Question 16
Analyze the code below. What does `f.seek(0)` do?
with open('data.txt', 'r') as f:
content = f.read()
f.seek(0)
content_again = f.read()Question 17
Which method is used to write a list of strings to a file?
Question 18
What is the difference between opening a file in `'t'` mode vs `'b'` mode?
Question 19
Analyze the code below. What happens if you iterate directly over the file object `f`?
with open('story.txt', 'r') as f:
for line in f:
passQuestion 20
Why is it important to specify the `encoding` parameter (e.g., `encoding='utf-8'`) when opening text files?
Question 21
What does the `tell()` method return?
Question 22
Analyze the code below. What is the correct way to strip the newline character from each line read?
with open('list.txt') as f:
line = f.readline()
clean_line = line.strip()Question 23
Can you use `with open(...)` to open multiple files at once?
with open('in.txt') as f1, open('out.txt', 'w') as f2:
passQuestion 24
What happens if you use `write()` on a file opened in `'a'` mode?
Question 25
Which exception is raised if you try to open a directory as a file?
Question 26
Analyze the code below. What is the value of `f.closed` after the block?
with open('data.txt', 'w') as f:
f.write('hi')
print(f.closed)Question 27
What is the purpose of the `buffering` argument in `open()`?
Question 28
Which method would you use to check if a file is readable?
Question 29
Analyze the code below. What happens?
f = open('new.txt', 'w')
f.write(123)Question 30
What is the correct way to read a file line by line and stop when a specific line is found?
with open('log.txt') as f:
for line in f:
if 'ERROR' in line:
print(line)
breakQuestion 31
Which of the following is NOT a valid file mode?
Question 32
What happens if you open a file using `open('file.txt', 'w+')`?
Question 33
How can you check the name of the file associated with a file object `f`?
Question 34
Analyze the code below. What is the content of `data`?
with open('file.txt', 'r') as f:
data = f.read(5)Question 35
What is the best way to handle file paths to ensure cross-platform compatibility?
