Bird
Raised Fist0

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

easy📝 Syntax Q3 of Q15
Python - File Handling Fundamentals
Which of the following is the correct syntax to open a file named 'data.txt' for reading in Python?
Afile = open('data.txt', 'a')
Bfile = open('data.txt', 'r')
Cfile = open('data.txt', 'w')
Dfile = open('data.txt', 'x')
Step-by-Step Solution
Solution:
  1. Step 1: Understand file modes

    'r' is for reading, 'w' for writing, 'a' for appending, 'x' for exclusive creation.
  2. Step 2: Choose mode for reading

    To read a file, use mode 'r'.
  3. Final Answer:

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

    Open for reading = mode 'r' [OK]
Quick Trick: Use 'r' mode to open files for reading [OK]
Common Mistakes:
MISTAKES
  • Using 'w' or 'a' to read files
  • Forgetting quotes around filename
  • Using 'x' which creates new file only

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes