Bird
Raised Fist0

What does the mode 'w' do when used with open in Python?

easy🧠 Conceptual Q11 of Q15
Python - File Handling Fundamentals
What does the mode 'w' do when used with open in Python?
AIt opens the file for writing and overwrites existing content.
BIt opens the file for reading only.
CIt appends new data to the end of the file.
DIt opens the file in binary mode.
Step-by-Step Solution
Solution:
  1. Step 1: Understand the 'w' mode in open()

    The 'w' mode opens a file for writing and clears existing content if the file exists.
  2. Step 2: Compare with other modes

    'r' is for reading, 'a' is for appending, and 'b' is for binary mode, so they don't match 'w'.
  3. Final Answer:

    It opens the file for writing and overwrites existing content. -> Option A
  4. Quick Check:

    open(file, 'w') overwrites file [OK]
Quick Trick: Remember 'w' means write and overwrite existing file [OK]
Common Mistakes:
MISTAKES
  • Confusing 'w' with 'a' (append mode)
  • Thinking 'w' opens file for reading
  • Assuming 'w' preserves old content

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes