Bird
0
0

Which file mode in Python opens a file for reading only, and raises an error if the file does not exist?

easy📝 Conceptual Q11 of 15
Python - File Handling Fundamentals
Which file mode in Python opens a file for reading only, and raises an error if the file does not exist?
file = open('data.txt', mode)
A"r"
B"w"
C"a"
D"x"
Step-by-Step Solution
Solution:
  1. Step 1: Understand the purpose of mode "r"

    Mode "r" opens a file for reading only and requires the file to exist.
  2. Step 2: Compare with other modes

    Mode "w" opens for writing (creates or truncates), "a" appends, and "x" creates a new file but errors if it exists.
  3. Final Answer:

    "r" -> Option A
  4. Quick Check:

    Read-only mode = "r" [OK]
Quick Trick: Read-only mode is just "r" [OK]
Common Mistakes:
  • Confusing "r" with "w" which overwrites files
  • Using "a" thinking it reads
  • Choosing "x" which creates files

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes