What this topic tests
- Choose r, w, a, and binary modes for the right job.
- Use a with block so the file closes even when an error occurs.
- Read lines and write text without losing newline characters.
Common mistakes
- Using w when you meant a and accidentally truncating the file.
- Opening a text file without an explicit encoding when the content is not ASCII.
- Forgetting that readlines keeps newline characters at the end of each line.
Sample questions
Ten representative questions from the full bank. Try each one before opening the answer.
-
1. Ming writes orders with open("orders.txt", "w"). What does "w" mean?
Show answer
Answer
B. write (overwrite existing content)Why: w opens for writing and truncates the file the moment open runs, so the old ledger is gone before the first write. Use a to keep it.
-
2. Ming writes a line, then opens the same file in another program and sees nothing. Which call pushes the buffer to disk?
Show answer
Answer
A. f.flush()Why: write() only hands text to the process buffer; flush() passes it to the OS, so another program sees nothing until then. close() flushes too, but only when the block ends.
-
3. Which structure closes a file even if an exception occurs inside the block?
Show answer
Answer
B. A with statementWhy: A with block is the only structure here tied to cleanup: its __exit__ runs close() while the exception is still propagating.
-
4. What is the main difference between f.read() and f.readlines()?
Show answer
Answer
B. the first returns one string; the second returns a list of linesWhy: read() returns the whole text as one string, while readlines() splits on newlines and keeps them, returning a list. readline() reads just one.
-
5. Ming reads a Chinese menu file and wants to avoid garbled text. What encoding goes in open(..., encoding=___)?
Show answer
Answer
utf-8Why: Leaving out encoding uses the platform default, often GBK on Windows; naming utf-8 decodes the bytes the way they were written.
-
6. Append to the end of a file. Which mode goes in the blank?
with open('orders.txt', '____', encoding='utf-8') as f: f.write('new\n')Show answer
Answer
aWhy: a appends at the end, so the previous orders survive. w would erase the file first and r refuses to write. If the file is missing, a creates it, so the first append needs no special case.
-
7. Write the header row before the data rows. Which method goes in the blank?
writer = csv.DictWriter(f, fieldnames=['name', 'price']) writer.____()Show answer
Answer
writeheaderWhy: writeheader() writes fieldnames as the first CSV row. Skip it and the file starts with data, so a later DictReader reads that first data row as its header.
-
8. pathlib splits a path. What are the three attributes?
from pathlib import Path p = Path("data/orders.csv") print(p.name, p.suffix, p.parent)Show answer
Answer
orders.csv .csv dataWhy: Path splits the string without touching the disk: name is the last part with its suffix, suffix is .csv including the dot, parent is data.
-
9. An in-memory file has two receipt lines. How many lines does readlines return?
import io f = io.StringIO('latte,18\namericano,15\n') print(len(f.readlines()))Show answer
Answer
2Why: readlines() returns one item per newline-terminated line, so 'latte,18\n' and 'americano,15\n' yield 2. readlines() also pulls the whole file into memory, which matters once the log grows large.
-
10. What is read back after writing to an in-memory file?
import io f = io.StringIO() f.write('latte,18') print(f.getvalue())Show answer
Answer
latte,18Why: StringIO keeps the written text in memory; getvalue() returns it unchanged, with no newline added and no disk involved. It behaves like a text file.
Common questions
Why use with open instead of open and close?
The with block closes the file automatically, including when an exception interrupts the block.
What does w mode do to an existing file?
It truncates the file first. Use a to append, or r+ if you need to update without truncating.
Keep the streak going.
The full bank keeps your place in this browser, filters by topic, and includes a daily question and mistake review.