Bird
0
0

Given this Python code on Raspberry Pi, what will be the content of 'log.txt' after running it twice?

medium📝 Command Output Q13 of 15
Raspberry Pi - Data Logging and Databases
Given this Python code on Raspberry Pi, what will be the content of 'log.txt' after running it twice?
with open('log.txt', 'a') as f:
    f.write('Temperature: 22\n')
ATemperature: 22
BTemperature: 22\n
CTemperature: 22\nTemperature: 22\n
DError: file not found
Step-by-Step Solution
Solution:
  1. Step 1: Understand file mode 'a' in Python

    Mode 'a' appends text to the file without deleting existing content.
  2. Step 2: Analyze running code twice

    Each run adds 'Temperature: 22\n' once, so two runs add it twice.
  3. Final Answer:

    Temperature: 22\nTemperature: 22\n -> Option C
  4. Quick Check:

    Append mode adds lines each run [OK]
Quick Trick: Mode 'a' appends, so repeated runs add lines [OK]
Common Mistakes:
MISTAKES
  • Thinking 'a' overwrites file
  • Ignoring newline characters
  • Expecting error when file exists

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes