1

I am not able to do a simpe pickle load on my VM. Here is simple demo code of that.

root@bn18-6:~# python
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pickle
>>> x = {1:2 }
>>> f = open ('demo', 'wb+')
>>> f
<open file 'demo', mode 'wb+' at 0x7fae71b44660>
>>> pickle.dump(x, f)
>>>
>>>
>>> f.close ()
>>>
>>>
>>> p = open('demo', 'wb+')
>>> p
<open file 'demo', mode 'wb+' at 0x7fae71b446f0>
>>> a = pickle.load (p)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/pickle.py", line 1384, in load
    return Unpickler(file).load()
  File "/usr/lib/python2.7/pickle.py", line 864, in load
    dispatch[key](self)
  File "/usr/lib/python2.7/pickle.py", line 886, in load_eof
    raise EOFError
EOFError

I have used pickling and unpickling several times before for larger data but didnt encounter this problem.

I have tried with 'r+','w+' too.

I have found This relevent but the solution involves increasing the RAM which is not possible in my case,

Also there are several questions dealing with pickle errors but most of them had problems with file open mode.

Dave
  • 11
  • 2
  • If you get the same behavior with `r`, **show us**, don't just assert it in the text. The failure with `w` is utterly unsurprising -- it's what one can/should expect. – Charles Duffy Oct 18 '17 at 20:51

1 Answers1

2

The filehandle you're trying to load (p) was opened with mode wb+. This truncates the file to length zero upon opening, and so there's nothing for pickle.load to unpickle, hence the EOFError. Perhaps you meant to use rb+ instead?

jwodder
  • 54,758
  • 12
  • 108
  • 124
  • I didnt know that it truncates the file. I replaces mode to `rb+` for pickle .dump and pickle.load and it works. thanks – Dave Oct 18 '17 at 20:53