Question 9
Back to Exercise
The program below stops with an error. Which statement correctly describes the problem?
import os, tempfile
os.chdir(tempfile.mkdtemp())
with open("result.txt", "w") as f:
f.write("old")
with open("result.txt", "x") as f:
f.write("new")
- A FileExistsError: "x" creates a new file and refuses to open one that exists; use "w" to overwrite deliberately Correct
- B It writes new over old
- C It appends new
- D PermissionError
Correct Answer: Option A
"x" exists precisely to prevent accidental overwrites.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!