Bird
0
0

This Python code is meant to log sensor data but fails to save anything. What is the error?

medium📝 Troubleshoot Q14 of 15
Raspberry Pi - Data Logging and Databases
This Python code is meant to log sensor data but fails to save anything. What is the error?
f = open('data.log', 'w')
f.write('Humidity: 45%')
f.close()
AThe file mode 'w' erases previous logs each run.
BMissing newline character causes no data saved.
CFile is not closed properly.
DThe write method syntax is incorrect.
Step-by-Step Solution
Solution:
  1. Step 1: Understand file mode 'w' behavior

    Mode 'w' overwrites the file, erasing previous logs each time.
  2. Step 2: Identify why logs seem missing

    Each run deletes old data, so only last run's data remains, seeming like no logs.
  3. Final Answer:

    The file mode 'w' erases previous logs each run. -> Option A
  4. Quick Check:

    'w' mode overwrites file [OK]
Quick Trick: Use 'a' mode to keep old logs, not 'w' [OK]
Common Mistakes:
MISTAKES
  • Thinking missing newline stops saving
  • Assuming file not closed causes no save
  • Believing write syntax is wrong

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes