[Tutor] Help! Pickle file
Alan Gauld
alan.gauld at btinternet.com
Thu Jul 5 11:43:49 CEST 2007
"Sara Johnson" <sarliz73 at yahoo.com> wrote
> This may sound silly, but when writing a program where
> there is a pickle file, how does that get included into the
> entire program? For instance;
>
> to create a new pickle file..
> ********************************************
> #!/usr/bin/python
> # Filename: pickling.py
>
> import cPickle as p
> Does this fall anywhere in the program in particular?
This simply imports the pickle module like any other.
As such its conventional to place the imports at the
top of the source file. However that doesn't do any pickling
or unpickling it just makes the name pickle accessible
to your program.
> I'm REALLY unfamiliar with this and I'm using Python
> for Dummies and Python.org.
Recall that the idea behind pickle is to make your data
persistent between program runs (or even during long
runs in case the program crashes). So you pickle the
data when you finish processing it and want to preserve
it safely on the disk for future use. You unpickle it again
when you need to use it or at the start of your program.
Typically the high level structure of your code might be:
import pickle
if pickle file exists:
data = pickle.load(pickle_file)
# process data here
pickle.dump(data)
sys.exit()
HTH,
--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld
More information about the Tutor
mailing list