File Access 104: pickle

The pickle module provides protocols for serializing Python objects – i.e., changing them into, or converting them back from, a stream of bytes. The purpose of pickle is either file storage (pickle.dump to write to a file object) or immediate access (pickle.dumps to create a bytes object). pickle is fast, relatively efficient, unsecure, can be backward compatible and can be compressed. DO NOT open a pickle file if you are not certain of its source. As of Python version 3.4, pickle is at Protocol version 4, so it is congruent with this web site’s objective of being 3.5 compatible. Version 4 can handle very large files.

On this page, we will only deal with the disk storage functions:
pickle.dump(object-to-pickle, save-to-file, protocol=3, fix_imports=True) – write a “pickled” obj to the open file. The disk file must be opened for binary writing.
…and…
pickle.load(file-to-read, *[, fix_imports=True][, encoding=”ASCII”][, errors=”strict”]) – retrieve a pickled object from an open file. The file must be opened for binary reading

The following Python objects are the most practical ones that can be pickled by an intermediate user:
integers, floating point numbers, complex numbers, strings, bytes, bytearrays, tuples, lists, sets, and dictionaries ( must contain only picklable objects).

Errors: pickle.PickleError , pickle.PicklingError, and pickle.UnpicklingError – others are frequent also

Example of writing and retrieving a pickled object:
Lets assume we import pickle with the intention of storing a list of primary colors at a location we will hold in the string variable “allPikPath.”

 import pickle
pikpath = "D:\\Documents\\Python3\\fileinout"
pikfile = "\\colors2.pickle"
allPikPath=pikpath + pikfile
colorlist = ['red','orange','yellow','green','blue','indigo','violet']

….that should get us started. Creating and writing the file is very straightforward:

with open(allPikPath,'wb') as colorspickeled:
    pickle.dump(colorlist,colorspickeled)

…. That’s all there is to it. And to retrieve the object into a list we name mylist and print it out:

with open(allPikPath,'rb') as pikobj:
    mylist = pickle.load(pikobj)
print(mylist)