How to add data to the end of a CSV file (Python) – Education Career Blog

I need to be able to periodically add data to the end of a CSV file. Ideally, I would like to do this without reading the entire file into memory.

Is there a way where I can append data to the end of a file?. One solution that occured to me was to simply shell a pipe command from Python, but that seemed too much of an ugly hack. is there a better way to append a line or maybe a few lines to the end of a CSV file?

,

Actually, there are two approaches. First, you can just open(filename, 'ab'), which will open the file for appending in binary mode. The second is using seek:

import os
my_file = open("data.csv", "ab")
my_file.seek(0, os.SEEK_END)

(I threw that second one in there mainly so I wasn’t just ripping off S. Lott and SilentGhost. It’s not particularly useful.)

Leave a Comment