Bird
0
0

What will be the content of file.txt after running this code if the file initially contains 'Data'?

medium📝 Predict Output Q4 of 15
Python - File Handling Fundamentals
What will be the content of file.txt after running this code if the file initially contains 'Data'?
with open('file.txt', 'a') as f:
    f.write('123')
AData
B123Data
C123
DData123
Step-by-Step Solution
Solution:
  1. Step 1: Understand append mode behavior

    Opening with 'a' adds new data at the end of existing content.
  2. Step 2: Write operation adds '123' after 'Data'

    The file content becomes 'Data' + '123' = 'Data123'.
  3. Final Answer:

    Data123 -> Option D
  4. Quick Check:

    Append adds at end = Data123 [OK]
Quick Trick: Appending adds data after existing content [OK]
Common Mistakes:
  • Thinking append overwrites
  • Assuming data is added at start
  • Ignoring initial file content

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes