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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With