Bird
0
0

Identify the error in this Python code for logging to CSV on Raspberry Pi:

medium📝 Troubleshoot Q6 of 15
Raspberry Pi - Data Logging and Databases
Identify the error in this Python code for logging to CSV on Raspberry Pi:
import csv
file = open('log.csv', 'a')
writer = csv.writer(file)
writer.writerow(['Time', 'Value'])
file.close()
AMissing newline='' in open() causes blank lines in CSV
BUsing 'a' mode instead of 'w' mode is incorrect
Ccsv.writer cannot write lists
Dfile.close() should be called before writer.writerow
Step-by-Step Solution
Solution:
  1. Step 1: Understand newline parameter in open()

    When writing CSV on Windows or Raspberry Pi, missing newline='' can add extra blank lines.
  2. Step 2: Check other options

    'a' mode is correct for appending, csv.writer accepts lists, and close() must be after writing.
  3. Final Answer:

    Missing newline='' in open() causes blank lines in CSV -> Option A
  4. Quick Check:

    Use newline='' to avoid blank lines [OK]
Quick Trick: Always use newline='' when opening CSV files for writing [OK]
Common Mistakes:
MISTAKES
  • Ignoring newline='' causing blank lines
  • Misusing file modes
  • Closing file too early

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes