How to read pickle file?

Kenenbek Arzymatov picture Kenenbek Arzymatov · Jan 28, 2016 · Viewed 176.9k times · Source

I created some data and stored it several times like this:

with open('filename', 'a') as f:
        pickle.dump(data, f)

Every time the size of file increased, but when I open file

with open('filename', 'rb') as f:
    x = pickle.load(f)

I can see only data from the last time. How can I correctly read file?

Answer

jsbueno picture jsbueno · Jan 28, 2016

Pickle serializes a single object at a time, and reads back a single object - the pickled data is recorded in sequence on the file.

If you simply do pickle.load you should be reading the first object serialized into the file (not the last one as you've written).

After unserializing the first object, the file-pointer is at the beggining of the next object - if you simply call pickle.load again, it will read that next object - do that until the end of the file.

objects = []
with (open("myfile", "rb")) as openfile:
    while True:
        try:
            objects.append(pickle.load(openfile))
        except EOFError:
            break