Home / Pickling (serialization) in Python edit
Try Documentalist, my app that offers fast, offline access to 190+ programmer API docs.

Pickling is an easy way to serialize data in Python. One possible use for that is preserving the state across script executions (like saving preferences).
There are few things worth knowing:
  • python has pickle and cPickle modules. They are almost the same (pickle handles more cases but cPickle is faster)
  • you can specify protocol parameter to dump() function. Use cPickle.HIGHEST_PROTOCOL - it’s the most efficient one
  • the simplest thing to do is to stuff everything you want to serialize in a hash and serialize the hash
The code snippet below shows how to save and load some data to a file. It removes the file if unpickling fails (which can happen if e.g. file is corrupted or not in the right format). The retry logic comes from experience - I found that os.remove() right after close() might fail.
import sys, os, string, time, cPickle

DATA_FILE_NAME = "settings.dat"

def saveData():
    fo = open(DATA_FILE_NAME, "wb")
    version = 1.0
    aString = "some data"
    cPickle.dump(version, fo, protocol = cPickle.HIGHEST_PROTOCOL)
    cPickle.dump(aString, fo, protocol = cPickle.HIGHEST_PROTOCOL)

def loadData():
        fo = open(DATA_FILE_NAME, "rb")
    except IOError:
        # it's ok to not have the file
        print "didn't find file %s with data" % DATA_FILE_NAME
        version = cPickle.load(fo)
        aString = cPickle.load(fo)
        removeRetryCount = 0
        while removeRetryCount < 3:
                time.sleep(1) # try to sleep to make the time for the file not be used anymore
                print "exception: n  %s, n  %s, n  %s n  when trying to remove file %s" % (sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2], filePath)
            removeRetryCount += 1

Feedback about page:

Optional: your email if you want me to get back to you:

Need fast, offline access to 190+ programmer API docs? Try my app Documentalist for Windows