Is there any way of checking if a file has been created by pickle
? I could just catch exceptions thrown by pickle.load
but there is no specific "not a pickle file" exception.
Pickle files don't have a header, so there's no standard way of identifying them short of trying to unpickle one and seeing if any exceptions are raised while doing so.
You could define your own enhanced protocol that included some kind of header by subclassing the Pickler()
and Unpickler()
classes in the pickle
module. However this can't be done with the much faster cPickle
module because, in it, they're factory functions, which can't be subclassed [1].
A more flexible approach would be define your own independent classes that used corresponding Pickler()
and Unpickler()
instances from either one of these modules in its implementation.
Update
The last byte of all pickle files should be the pickle.STOP
opcode, so while there isn't a header, there is effectively a very minimal trailer which would be a relatively simple thing to check.
Depending on your exact usage, you might be able to get away with supplementing that with something more elaborate (and longer than one byte), since any data past the STOP
opcode in a pickled object's representation is ignored [2].
pickle.loads()
, which also applies to pickle.load()
since it's currently implemented in terms of the former. There is no sure way other than to try to unpickle it, and catch exceptions.
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