Bird
0
0

What will be the output of this Python code on Raspberry Pi?

medium📝 Predict Output Q4 of 15
Raspberry Pi - Data Logging and Databases
What will be the output of this Python code on Raspberry Pi?
with open('log.txt', 'w') as f:
    f.write('Temp: 22C\n')
with open('log.txt', 'a') as f:
    f.write('Humidity: 60%\n')
with open('log.txt', 'r') as f:
    print(f.read())
AError: file not found
BHumidity: 60%
CTemp: 22C
DTemp: 22C Humidity: 60%
Step-by-Step Solution
Solution:
  1. Step 1: Analyze file write modes

    First write uses 'w' which creates or overwrites file with 'Temp: 22C\n'.
  2. Step 2: Analyze append and read

    Second write uses 'a' to add 'Humidity: 60%\n' without deleting previous data. Reading prints both lines.
  3. Final Answer:

    Temp: 22C Humidity: 60% -> Option D
  4. Quick Check:

    Write 'w' then append 'a' = both lines [OK]
Quick Trick: Write 'w' clears file, 'a' adds data [OK]
Common Mistakes:
MISTAKES
  • Assuming 'a' overwrites file
  • Thinking 'w' appends data
  • Expecting error when file exists

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes