Pickle a file object
Michael Stenner
mstenner at phy.duke.edu
Mon Dec 2 15:00:24 EST 2002
On Mon, Dec 02, 2002 at 07:56:14PM +0100, Martin v. Löwis wrote:
> "Gumuz" <gumuz at looze.net> writes:
>
> > so what _is_ a prefered way to embed a file in a 'picklable' datastructure?
>
> I would suggest not to pickle file objects in picklable datatructures.
That's very helpful :)
I suggest you pickle the "important" information about the file.
What's "important" depends on your application. It may simply by the
file name. It may be the modes with which the file was opened. it
may include your current position within the file [fo.tell()] and so
on.
It shouldn't be hard to create a couple of functions
(pickle_file_object and unpickle_file_object), such that
pickle_file_object takes a file object and returns an object (dict,
list, class instance, whatever) that contains all the necessary info.
unpickle_file_object could then take that info, open the file and
return you to the same place.
-Michael
--
Michael Stenner Office Phone: 919-660-2513
Duke University, Dept. of Physics mstenner at phy.duke.edu
Box 90305, Durham N.C. 27708-0305
More information about the Python-list
mailing list