Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EOFError Ran out of input Python

I am trying to use pickle to create a save file for my game, but When I try to submit my dictionary, and then take that information back, but it isn't working.

import pickle
data = {'health':100, 'gold':1560, 'name': 'mariano'}
with open('s.txt','wb') as f:
    pickle.dump(data, f, protocol = 2)

with open('s.txt','rb') as f:
    data = pickle.load(f)

then when I run that code it gives me this error

EOFError: Ran out of input
like image 540
Travis Avatar asked Mar 09 '23 10:03

Travis


2 Answers

I figured out what went wrong, or at least how I fixed the situation. You were trying to use a file that was already pickled, and since it ran with an error it broke it, all you have to do is change the name of the file.

like image 144
Travis Avatar answered Mar 20 '23 08:03

Travis


I was having a similar issue. My code looked like this:

h = open("my_file.pkl", "wb")
pickle.dump(my_data, h)

That was within a cell in a Jupyter notebook. When trying to load my_file.pkl later I'd get EOFError: Ran out of input.

I believe the cause of the error was the file handler h wasn't getting closed, meaning the file didn't save correctly. Changing it to this fixed the problem:

with open("my_file.pkl", "wb") as h:
    pickle.dump(my_data, h)
like image 37
GMA Avatar answered Mar 20 '23 10:03

GMA