[Tutor] Closeing a file written to by cPickle dump
Dave S
pythontut at pusspaws.net
Sun Nov 28 01:17:21 CET 2004
Kent Johnson wrote:
> 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()
Thanks for the above, now in my code :)
Dave
More information about the Tutor
mailing list