Practice: File Modes
Q1. Create with x
Write code that creates `new.txt` with mode "x", writes `hi`, and prints `ok`.
Goal: Use x mode
Q2. Catch FileExistsError
Create `z.txt` with "w". Then try to open it with "x" and print `exists` if it fails.
Goal: Handle x errors
Q3. Binary write and read
Write the bytes 65, 66, 67 to `b.bin` and read them back as a list of numbers.
Goal: Binary mode basics
Q4. Use r+ to patch
Write `hello world` to `r.txt`, then use r+ to change the first five letters to `HELLO` and print the file.
Goal: Update with r+
Q5. Mode checker
Open `chk.txt` with "w" and print whether it is readable and writable.
Goal: Inspect file objects
Q6. Safe save function
Write save(name, text) that uses "x" and returns True if saved or False if the file already existed. Call it twice with the same name and print both results.
Goal: Protect against overwriting
Q7. Copy a binary file
Write 4 bytes to `src.bin`, copy it to `dst.bin` using rb and wb, then print whether the contents are equal.
Goal: Copy with binary modes
Q8. w+ then read
Open `w.txt` with "w+", write `data`, seek to the start, and print the content.
Goal: Use w+
Q9. Empty by opening
What does this print?
with open("e.txt", "w") as f:
f.write("keep me")
with open("e.txt", "w") as f:
pass
with open("e.txt") as f:
print(repr(f.read()))Goal: Danger of w
Q10. r+ overwrite
What does this print?
with open("o.txt", "w") as f:
f.write("12345")
with open("o.txt", "r+") as f:
f.write("ab")
f.seek(0)
print(f.read())Goal: Predict r+
Q11. x on existing
What does this print?
open("t.txt", "w").close()
try:
open("t.txt", "x")
except FileExistsError:
print("blocked")
else:
print("made")Goal: Predict x
Q12. Bytes display
What does this print?
with open("y.bin", "wb") as f:
f.write(b"Hi")
with open("y.bin", "rb") as f:
print(f.read())Goal: Predict bytes output
Q13. Mode attribute
What does this print?
with open("k.txt", "w") as f:
pass
with open("k.txt", "rb") as f:
print(f.mode, f.readable())Goal: Read mode attribute
Q14. Wrong type
What does this print?
try:
with open("q.bin", "wb") as f:
f.write("text")
except TypeError:
print("need bytes")Goal: Type must match mode
Q15. a+ read after seek
What does this print?
with open("u.txt", "w") as f:
f.write("A")
with open("u.txt", "a+") as f:
f.write("B")
f.seek(0)
print(f.read(), f.readable())Goal: Predict a+
Q16. Reading a missing file
This crashes because the file does not exist. Fix it so the output is `1` (create it first).
with open("missing.txt", "r+") as f:
f.write("1")Goal: Pick a mode that creates
Q17. Wiped data
The program should print `5` but prints `0`. Fix it.
with open("data.txt", "w") as f:
f.write("5")
with open("data.txt", "w") as f:
value = f.read()
print(value)Goal: Do not open w to read
Q18. Bytes versus str
This code raises TypeError. Fix it to print 3.
with open("n.bin", "wb") as f:
print(f.write("abc"))Goal: Encode for binary
Q19. Invalid mode
`open("f.txt", "rw")` fails. Fix it so it prints `ok` after opening for reading and writing an existing file.
open("f.txt", "rw")Goal: Know valid modes
Q20. Explain w versus x
Explain in two or three sentences how "w" and "x" differ and when x is better.
Goal: Compare w and x