You can also use joblib
to read pickle files. It is especially useful if you were reading pickled scikit-learn models or numpy ndarray objects (joblib comes with scikit-learn and is specifically designed to handle numpy ndarrays).
import joblib
x = joblib.load("my_file.pkl")
Then again, both joblib
and pandas
use the pickle.load
from the standard library, so in reality, both are almost the same as:
with open("my_file.pkl", "rb") as f:
x = pickle.load(f)
It's just that file handling and some backward compatibility considerations are handled under the hood in pandas and joblib.
In particular, for the OP's specific case, they cannot work and must use the same try-except block to read all objects, e.g.:
objects = []
with open("myfile", "rb") as openfile:
while True:
try:
objects.append(pd.read_pickle(openfile))
except EOFError:
break
open('filename', 'wb')
– Tailspin