Bird
0
0

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

easy📝 Syntax Q3 of 15
Python - Standard Library Usage
Which of the following is the correct syntax to open a file named 'log.txt' for reading in Python?
Aopen('log.txt', 'w')
Bopen('log.txt', 'a')
Copen('log.txt', 'r')
Dopen('log.txt', 'x')
Step-by-Step Solution
Solution:
  1. Step 1: Identify the mode for reading

    The mode 'r' is used to open a file for reading only.
  2. Step 2: Check other modes

    'w' is for writing (overwrites), 'a' is for appending, and 'x' is for creating a new file and writing. So only 'r' fits reading.
  3. Final Answer:

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

    Read mode = 'r' [OK]
Quick Trick: Use 'r' mode to read files safely [OK]
Common Mistakes:
  • Using 'w' instead of 'r'
  • Confusing 'a' with read mode
  • Using 'x' which fails if file exists

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes