Bird
0
0

Given log.txt initially contains 'Start\n', what will be printed after running this code?

medium📝 Predict Output Q5 of 15
Python - File Handling Fundamentals
Given log.txt initially contains 'Start\n', what will be printed after running this code?
with open('log.txt', 'a') as f:
    f.write('End\n')
with open('log.txt', 'r') as f:
    print(f.read())
AStartEnd
BEnd Start
CStart End
DEnd
Step-by-Step Solution
Solution:
  1. Step 1: Initial file content

    The file contains 'Start\n' initially.
  2. Step 2: Append 'End\n'

    Opening in append mode adds 'End\n' at the end.
  3. Step 3: Reading file

    Reading the file prints all content: 'Start\nEnd\n'.
  4. Final Answer:

    Start End -> Option C
  5. Quick Check:

    Append adds at end preserving existing lines [OK]
Quick Trick: Appending adds text after existing content [OK]
Common Mistakes:
  • Assuming append overwrites content
  • Confusing order of lines
  • Ignoring newline characters

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes