Bird
Raised Fist0

Why does opening a file in mode "r" raise an error if the file does not exist?

hard🧠 Conceptual Q10 of Q15
Python - File Handling Fundamentals
Why does opening a file in mode "r" raise an error if the file does not exist?
ABecause "r" mode requires the file to exist for reading
BBecause "r" mode creates a new empty file
CBecause "r" mode truncates the file
DBecause "r" mode appends to the file
Step-by-Step Solution
Solution:
  1. Step 1: Understand "r" mode behavior

    "r" mode opens file for reading only and expects the file to exist.
  2. Step 2: Reason about error cause

    If file is missing, "r" mode cannot open it and raises FileNotFoundError.
  3. Final Answer:

    "r" mode requires existing file for reading -> Option A
  4. Quick Check:

    Read mode needs existing file [OK]
Quick Trick: Read mode errors if file missing [OK]
Common Mistakes:
MISTAKES
  • Thinking "r" creates file
  • Confusing truncate with read
  • Assuming append in read mode

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes