14

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?

shx2
  • 61,779
  • 13
  • 130
  • 153
Surya
  • 4,824
  • 6
  • 38
  • 63
  • When you created the pickle object, what OS did you create it on? Linux or Windows? I dont think you could create a pickle object on one system and load it from other – Rush Mar 30 '13 at 15:04
  • @Rush The code was initially written on Linux.. I (new dev) working in windows... but since its a Django project, I generated everything right on my system again.. – Surya Mar 30 '13 at 15:07
  • @Rush, sure you can. as long as you open files in binary mode on both ends, to avoid newline incompatibility. – shx2 Mar 30 '13 at 15:10
  • Possible duplicate of [python 2.6 cPickle.load results in EOFError](http://stackoverflow.com/questions/2187558/python-2-6-cpickle-load-results-in-eoferror) – sds Aug 31 '16 at 23:04

1 Answers1

25

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.

shx2
  • 61,779
  • 13
  • 130
  • 153