[Tutor] Closeing a file written to by cPickle dump

Kent Johnson kent37 at tds.net
Sat Nov 27 22:13:31 CET 2004


Dave S wrote:
> I need to use pickle to save dictionaries, I have started:
> 
> .....
> from cPickle import dump
> ......
> dump(dict,open(data_dir+'/'+save_name,'w'))
> 
> It appears to work OK & I can read from the pickle but in this dump 
> format do I need to close the open file with an equiverlant of 
> xxx.close(). Am I in danger of data not being flushed to the hard drive ?

Though it may not always be necessary,  I think it is prudent to always 
close a file that was opened for writing. The call to open returns a 
file object, you just have to retain a reference to it so you can close 
it when you are done:

f=open(data_dir+'/'+save_name,'w'
dump(dict,f))
f.close()

> 
> If I do need to close it, how do I do that because there is no class 
> instance ?
> 
> Cheers
> Dave
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


More information about the Tutor mailing list