tags:

views:

54

answers:

1

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?

A: 

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.)

detly
Erm, unless I'm mistaken, you can't append to a file opened in read mode? - am I missing something here?
skyeagle
According to that tutorial link: "`r+` opens the file for both reading and writing". According to [the docs](http://docs.python.org/library/functions.html#open), it's for "updating." Um...
detly
@detly: Thanks for the clarification. Probably stick to 'ab' mode just to make sure the data gets appended.
skyeagle
@skyeagle - honestly, I have no idea what it means by "updating." I'd use 'ab' too (changing answer now).
detly