> I think you should close that. The following works fine in the
> notebook. It makes no sense to close the file -- instead, you have
> to delete the csv writer object, which flushes it to the file.
This isn't guaranteed to flush it to the file, though, because del
only deletes the name. That it results in a flush is entirely a
cpython implementation detail because there are no live references to
it. Looking at the test.csv file after `del output`:
mcneil@ubuntu:~/coding$ python csvflush.py
['1,2\r\n', '3,4\r\n', '5,6\r\n']
mcneil@ubuntu:~/coding$ pypy csvflush.py
[]
Or more specifically in cpython:
import csv
fp = open("test.csv", "w")
output=csv.writer(fp)
data=[[1,2],[3,4],[5,6]]
for r in data:
output.writerow(r)
print(open('test.csv').readlines())
del output
print(open('test.csv').readlines())
del fp
print(open('test.csv').readlines())
produces
[]
[]
['1,2\r\n', '3,4\r\n', '5,6\r\n']
where nothing happens until the last reference to the *file* is lost,
which in cpython gets it closed.
I'd either call .flush() and/or .close() explicitly or wrap it in a
with block so that I don't have to remember.
Doug
--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/sage-support
URL: http://www.sagemath.org