Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pickle.load() raising EOFError in Windows

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?

like image 656
Surya Avatar asked Mar 30 '13 15:03

Surya


People also ask

What does pickle load function do?

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.

How do you load objects from pickles?

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 .

Why is python pickle insecure?

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.

How do I reduce the size of a pickle file?

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.


1 Answers

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.

like image 199
shx2 Avatar answered Oct 11 '22 02:10

shx2