[Tutor] Closeing a file written to by cPickle dump

Liam Clarke cyresse at gmail.com
Sun Nov 28 00:10:00 CET 2004


Hi all, 

Pickle... just reading O'Reilly's Python In A Nutshell, and thought
this would be an opportune time to clarify pickling. Pickle will write
dictionaries/classes/functions to disk, and then read them back in a
usable manner? I'm just trying to paraphrase this module's
functionality, as O'Reilly states -

"The pickle and cPickle modules supply factory functions, named
Pickler and Unpickler, to generate objects that wrap file-like objects
and supply serialization mechanisms."

Which makes me say 'Wuzzah?'
If so, how silly am I, creating functions to load/save dictionaries
myself, when there already exists functions to do that, plus save
other useful stuff.

Regards,

Liam Clarke


On Sat, 27 Nov 2004 16:13:31 -0500, Kent Johnson <kent37 at tds.net> 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()
> 
> 
> 
> >
> > 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
> >
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


-- 
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.


More information about the Tutor mailing list