Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get number of objects in a pickle?

Is there a short way to get number of objects in pickled file - shorter than writing a function that opens the file, keeps calling pickle.load method and updating num_of_objs by 1 until it catches EOFError and returns the value?

like image 511
morris Avatar asked Jan 13 '23 11:01

morris


1 Answers

No, there isn't. The pickle format does not store that information.

If you need that type of metadata, you need to add it to the file yourself when writing:

pickle.dump(len(objects), fileobj)
for ob in objects:
    pickle.dump(ob, fileobj)

Now the first record tells you how many more are to follow.

like image 87
Martijn Pieters Avatar answered Jan 17 '23 16:01

Martijn Pieters