Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: pickle.load() raising EOFError

I have a pickle file using .txt format. I want to load this pickle file with python 2.7. The size is 438.5 MB. This is how I load data :

def readpickle(path="C:/Python27/Lib/site-packages/xy/"):
with open(path+"filenamereal2.txt","rb") as f:
    model = pickle.load(f)

return model

And I get this error

    itemmodelreal=readpickle(path="C:/Users/Lab Komputasi/Documents/estu/") 
Traceback (most recent call last):
File "<ipython-input-33-265e46f74915>", line 1, in <module>
    itemmodelreal=readpickle(path="C:/Users/Lab Komputasi/Documents/estu/")

  File "<ipython-input-31-fbd3e8b9e043>", line 3, in readpickle
    model = pickle.load(f)

  File "C:\Users\Lab Komputasi\Anaconda2\lib\pickle.py", line 1384, in load
    return Unpickler(file).load()

  File "C:\Users\Lab Komputasi\Anaconda2\lib\pickle.py", line 864, in load
    dispatch[key](self)

  File "C:\Users\Lab Komputasi\Anaconda2\lib\pickle.py", line 886, in load_eof
    raise EOFError

EOFError

this is the code that i use to write pickle :

 with open("filenamereal3.txt", "wb") as f:
    pickle.dump(result, f)
f.close()

I have used read binary ('rb') to load and write binary ('wb') to write, but it's still have that error. Do you have any idea why it's still error? how can i solve this error?

Thank you for your help....

like image 725
estu Avatar asked May 26 '16 03:05

estu


People also ask

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 load pickled data in Python?

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.

What is Pickle dump ()?

The dump() method of the pickle module in Python, converts a Python object hierarchy into a byte stream. This process is also called as serilaization. The converted byte stream can be written to a buffer or to a disk file.

What does pickle load do in Python?

Pickle in Python is primarily used in serializing and deserializing a Python object structure. In other words, it's the process of converting a Python object into a byte stream to store it in a file/database, maintain program state across sessions, or transport data over the network.


2 Answers

I encountered the same error while loading a big file dumped in highest protocol.

This seems to be a bug of the pickle library. I solved it using cPickle instead.

import cPickle as pickle
like image 87
David Avatar answered Sep 20 '22 06:09

David


To load data, wouldn't you want to be reading data ("rb") instead of writing data ("wb")?

Loading data should look like this:

 with open("C:/Users/Lab Komputasi/Documents/estu/filenamereal1.txt", "rb") as f:
     data = pickle.load(f)

Also, using f.close() is unnecessary because you are using a with/as clause.

like image 22
Neil Chowdhury o_O Avatar answered Sep 22 '22 06:09

Neil Chowdhury o_O