Practice: File Modes

« Read the chapter

Q1. Create with x

BeginnerCoding

Write code that creates `new.txt` with mode "x", writes `hi`, and prints `ok`.

Goal: Use x mode

Q2. Catch FileExistsError

BeginnerCoding

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

BeginnerCoding

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

IntermediateCoding

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

BeginnerCoding

Open `chk.txt` with "w" and print whether it is readable and writable.

Goal: Inspect file objects

Q6. Safe save function

IntermediateCoding

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

AdvancedCoding

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

IntermediateCoding

Open `w.txt` with "w+", write `data`, seek to the start, and print the content.

Goal: Use w+

Q9. Empty by opening

BeginnerOutput

What does this print?

Code
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

IntermediateOutput

What does this print?

Code
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

BeginnerOutput

What does this print?

Code
open("t.txt", "w").close()
try:
    open("t.txt", "x")
except FileExistsError:
    print("blocked")
else:
    print("made")

Goal: Predict x

Q12. Bytes display

IntermediateOutput

What does this print?

Code
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

BeginnerOutput

What does this print?

Code
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

AdvancedOutput

What does this print?

Code
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

IntermediateOutput

What does this print?

Code
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

BeginnerDebugging

This crashes because the file does not exist. Fix it so the output is `1` (create it first).

Code
with open("missing.txt", "r+") as f:
    f.write("1")

Goal: Pick a mode that creates

Q17. Wiped data

IntermediateDebugging

The program should print `5` but prints `0`. Fix it.

Code
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

IntermediateDebugging

This code raises TypeError. Fix it to print 3.

Code
with open("n.bin", "wb") as f:
    print(f.write("abc"))

Goal: Encode for binary

Q19. Invalid mode

BeginnerDebugging

`open("f.txt", "rw")` fails. Fix it so it prints `ok` after opening for reading and writing an existing file.

Code
open("f.txt", "rw")

Goal: Know valid modes

Q20. Explain w versus x

IntermediateConceptual

Explain in two or three sentences how "w" and "x" differ and when x is better.

Goal: Compare w and x