We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Appending to CSV Files

Sometimes you want to create a brand-new CSV file, but sometimes you want to keep the existing file and append more rows to the end.

If you open a file in "a" (append) mode, Python appends new content instead of overwriting the file:

with open("posts.csv", "a", newline="") as file:
    writer = csv.DictWriter(file, fieldnames=["post_id", "likes", "published"])
    writer.writerow({"post_id": "post-12", "likes": "350", "published": "true"})

When appending, don't call writeheader() again unless you're creating a brand-new file. You could wind up with duplicate headers in the middle of your CSV:

post_id,likes,published
post-01,1200,true
post_id,likes,published
post-12,350,true

Assignment

The ops team has an existing CSV file with device data. You need to append new readings without overwriting what's already there.

Complete the append_multiple_readings function. It accepts a list of device dictionaries and appends them to an existing CSV file at the provided path.