Bird
0
0

You run this code on your Raspberry Pi but the CSV file remains empty:

medium📝 Troubleshoot Q14 of 15
Raspberry Pi - Data Logging and Databases
You run this code on your Raspberry Pi but the CSV file remains empty:
import csv
file = open('log.csv', 'w')
writer = csv.writer(file)
writer.writerow(['Date', 'Value'])
file.close()

What is the likely problem?
AThe file was not closed properly
BThe newline parameter is missing in open() causing blank lines
CThe file was not opened in append mode
DThe writerow method was not called correctly
Step-by-Step Solution
Solution:
  1. Step 1: Check file opening and writing

    File is opened in write mode and writerow is called correctly.
  2. Step 2: Identify missing newline='' causing blank lines on Windows/Linux

    Without newline='', csv.writer may write extra blank lines or cause issues making file appear empty.
  3. Final Answer:

    The newline parameter is missing in open() causing blank lines -> Option B
  4. Quick Check:

    Use newline='' to avoid blank lines in CSV [OK]
Quick Trick: Always use newline='' when opening CSV files for writing [OK]
Common Mistakes:
MISTAKES
  • Assuming append mode is required to write
  • Forgetting to close the file (but here it is closed)
  • Thinking writerow syntax is wrong

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes