LearnFrenzy
Sign in Take a free test

File Handling · Find the Error · Question 9

Error: "x" mode on an existing file Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 9 Back to Exercise

The program below stops with an error. Which statement correctly describes the problem?

main.py
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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links