Bird
0
0

Which of the following is the correct syntax to open a file named log.txt for appending text data?

easy📝 Syntax Q12 of 15
Python - File Handling Fundamentals
Which of the following is the correct syntax to open a file named log.txt for appending text data?
Aopen('log.txt', 'x')
Bopen('log.txt', 'r')
Copen('log.txt', 'w')
Dopen('log.txt', 'a')
Step-by-Step Solution
Solution:
  1. Step 1: Identify the mode for appending

    The mode 'a' is used to open a file for appending data.
  2. Step 2: Check other modes

    'r' is for reading, 'w' is for writing (overwrites), 'x' is for exclusive creation.
  3. Final Answer:

    open('log.txt', 'a') -> Option D
  4. Quick Check:

    Append mode syntax = open(filename, 'a') [OK]
Quick Trick: Use 'a' mode to append, not 'r', 'w', or 'x' [OK]
Common Mistakes:
  • Using 'w' which erases file content
  • Using 'r' which does not allow writing
  • 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