Saving and loading multiple objects in pickle file?

Two additions to Tim Peters’ accepted answer.

First, you need not store the number of items you pickled separately if you stop loading when you hit the end of the file:

def loadall(filename):
    with open(filename, "rb") as f:
        while True:
            try:
                yield pickle.load(f)
            except EOFError:
                break

items = loadall(myfilename)

This assumes the file contains only pickles; if there’s anything else in there, the generator will try to treat whatever else is in there as pickles too, which could be dangerous.

Second, this way, you do not get a list but rather a generator.
This will load only one item into memory at a time, which is useful
if the dumped data is very large — one possible reason why you may
have wanted to pickle multiple items separately in the first place.
You can still iterate over items with a for loop as if it were
a list.

Leave a Comment