Bird
0
0

Which of the following is the correct syntax to open a file named data.txt for reading using with?

easy📝 Syntax Q12 of 15
Python - Context Managers

Which of the following is the correct syntax to open a file named data.txt for reading using with?

?
Aopen with('data.txt', 'r') as file:
Bwith open('data.txt') file:
Cwith open('data.txt', 'r') as file:
Dwith open('data.txt', 'read') as file:
Step-by-Step Solution
Solution:
  1. Step 1: Recall correct open syntax

    The correct way to open a file for reading is open(filename, 'r').
  2. Step 2: Check with statement syntax

    The with statement requires with open(...) as variable: format.
  3. Final Answer:

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

    Correct with open syntax = with open('data.txt', 'r') as file: [OK]
Quick Trick: Use with open(filename, 'r') as var: [OK]
Common Mistakes:
  • Omitting 'as' keyword
  • Using 'read' instead of 'r' mode
  • Wrong order of keywords

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes