Bird
Raised Fist0

Which Python statement correctly opens a file named 'notes.txt' for reading line by line?

easy🧠 Conceptual Q2 of Q15
Python - File Reading and Writing Strategies
Which Python statement correctly opens a file named 'notes.txt' for reading line by line?
Aopen('notes.txt', 'x') as file:
Bwith open('notes.txt', 'r') as file:
Cwith open('notes.txt', 'a') as file:
Dopen('notes.txt', 'w') as file:
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct mode for reading

    'r' mode opens file for reading, which is needed to read lines.
  2. Step 2: Use 'with' statement for safe file handling

    'with' ensures file closes automatically after reading.
  3. Final Answer:

    with open('notes.txt', 'r') as file: -> Option B
  4. Quick Check:

    Open file for reading = D [OK]
Quick Trick: Use 'r' mode with 'with' to read files safely [OK]
Common Mistakes:
MISTAKES
  • Using 'w' or 'a' which are for writing/appending
  • Not using 'with' statement
  • Forgetting quotes around filename

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes