[Tutor] Help! Pickle file

John Fouhy john at fouhy.net
Thu Jul 5 06:57:41 CEST 2007


On 05/07/07, 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;

Hi Sara,

You create pickles with pickle.dump and you read them with pickle.load.

For example:

####### create.py #######

import pickle

data = ['one', 'two', 'three']
outfile = open('data.pickle', 'wb')
pickle.dump(data, outfile)
outfile.close()
#################

######## load.py ########

import pickle
infile = open('data.pickle')
data = pickle.load(infile)
print data
#################

If you run the first script, it will create a pickle of the list
['one', 'two', 'three'].  You'll be able to see the file in the
directory where you ran the script; you can even look at it if you
like.  The second script will read the pickle and reconstruct the
list.

Hope this helps!

-- 
John.


More information about the Tutor mailing list