Bird
0
0

Which of the following is the correct way to open a file named data.txt for reading in Python?

easy📝 Syntax Q12 of 15
Python - File Handling Fundamentals
Which of the following is the correct way to open a file named data.txt for reading in Python?
Aopen('data.txt', 'w')
Bopen('data.txt', 'x')
Copen('data.txt', 'r')
Dopen('data.txt', 'a')
Step-by-Step Solution
Solution:
  1. Step 1: Recall file modes in Python

    'r' mode opens a file for reading, 'w' for writing, 'x' for creating, 'a' for appending.
  2. Step 2: Match mode with reading requirement

    Since we want to read the file, 'r' mode is correct.
  3. Final Answer:

    open('data.txt', 'r') -> Option C
  4. Quick Check:

    Read mode = 'r' [OK]
Quick Trick: Use 'r' mode to open files for reading [OK]
Common Mistakes:
  • Using 'w' which overwrites file
  • Using 'a' which appends instead of reading
  • Confusing 'x' with reading mode

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes