tags:

views:

36

answers:

2

Python newbie getting a bit frustrated with the csv module. At this rate, it would have been easier if I wrote the file parser myself, but I want to do things the Pythonic way ....

I have written a little python script that should save my data into a CSV file.

Here is a snippet of my code:

import csv

wrtr = csv.writer(open('myfile.csv','wb'),delimiter=',', quotechar='"')

for row in rows:
    wrtr.writerow([row.field1,row.field2,row.field3])

The file myfile.csv is created successfully, yet it is empty - but has a lock on it, as its still being used by the Python process. It seems that the data has been written to the file in memory, but it has not yet been flushed to disk.

Since the Python process is holding a lock on the file, then I assume that I am responsible for releasing the lock. Here are my questions:

  1. How do I get python to flush to disk
  2. How do I close the file that was opened in the csv.writer() method?
+2  A: 

The flush() and close() methods of the file object. Or use with.

Ignacio Vazquez-Abrams
But I am creating an 'anonymous' file object - so I have nothing to call flush and close against. Unless I am mistaken, this means that I will have to modify the code slightly to cleate a file object first before passing it to the csv.writer() function. I can then call flush() and close() on the file object as you suggested. If that is the case, then the documentation at http://docs.python.org/library/csv.html is indeed as useless/misleading as I think it is, and I will NEVER refer to it again.
skyeagle
Works fine here. It could be something screwy that Windows is doing.
Ignacio Vazquez-Abrams
"But I am creating an 'anonymous' file object". Don't.
S.Lott
To @skyeagle's credit, many of the examples in the docs he linked to do this. This is unpythonic, I'd say ("explicit is better than implicit"), but I wouldn't go so far as to take the consequence of never looking at the docs again. :)
Tim Pietzcker
@Tim: had to give you a +1 for seeing my point of view :). Regarding the never looking at the Python docs again, once the "red mist" has settled, I will be fine .... :)
skyeagle
+1  A: 

Use

with open('myfile.csv','wb') as myfile:
    wrtr = csv.writer(myfile, delimiter=',', quotechar='"')
    for row in rows:
        wrtr.writerow([row.field1,row.field2,row.field3])
        myfile.flush() # whenever you want

or

myfile = open('myfile.csv','wb')
wrtr = csv.writer(myfile, delimiter=',', quotechar='"')
for row in rows:
    wrtr.writerow([row.field1,row.field2,row.field3])
    myfile.flush() # whenever you want, and/or
myfile.close() # when you're done.

The nice thing about the first approach is that your file will also be automatically properly closed in case of an Exception.

If you want your file object to be anonymous, then it will only be closed when the program exits. When or whether it is flushed depends on the OS - so it might be never until exit.

Tim Pietzcker
Thanks for the EXPLICIT code. If all Python examples on the net were this well written - it would make life SO much easier ... :)
skyeagle
Hey, it's your code - I just added a few characters :)
Tim Pietzcker