Bird
0
0

What will be the content of 'log.txt' after running this code?

medium📝 Predict Output Q4 of 15
Python - File Handling Fundamentals
What will be the content of 'log.txt' after running this code?
with open('log.txt', 'w') as f:
    f.write('Start\n')
with open('log.txt', 'a') as f:
    f.write('End\n')
AStartEnd
BEnd\n
CStart\n
DStart\nEnd\n
Step-by-Step Solution
Solution:
  1. Step 1: Analyze first write with 'w' mode

    Opening with 'w' erases file and writes 'Start\n'.
  2. Step 2: Analyze second write with 'a' mode

    Opening with 'a' appends 'End\n' after existing content.
  3. Final Answer:

    Start\nEnd\n -> Option D
  4. Quick Check:

    'w' overwrites, 'a' appends [OK]
Quick Trick: Use 'w' to overwrite, 'a' to append [OK]
Common Mistakes:
  • Assuming 'a' overwrites
  • Ignoring newline characters
  • Confusing order of writes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes