This is how the code is
with open(pickle_f, 'r') as fhand:
obj = pickle.load(fhand)
This works fine on Linux systems but not on Windows. Its showing EOFError.
I have to use rb
mode to make it work on Windows.. now this isn't working on Linux.
Why this is happening, and how to fix it?
Python Pickle load To retrieve pickled data, the steps are quite simple. You have to use pickle. load() function to do that. The primary argument of pickle load function is the file object that you get by opening the file in read-binary (rb) mode.
load() to load an object from a file. Call open(file, "rb") with the filename of the stored Pickle object as file to open the Pickle file for reading in binary. Use pickle. load(file) to load the object from file .
Cons-1: Pickle is Unsafe Unlike JSON, which is just a piece of string, it is possible to construct malicious pickle data which will execute arbitrary code during unpickling . Therefore, we should NEVER unpickle data that could have come from an untrusted source, or that could have been tampered with.
Enter the bz2 library for python, which enables bz2 compression for any file. By sacrificing some of the speed gained by pickling your data, you can compress it to a quarter of its original size.
Always use b
mode when reading and writing pickles (open(f, 'wb')
for writing, open(f, 'rb')
for reading). To "fix" the file you already have, convert its newlines using dos2unix
.
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