I am trying to save and load objects using pickle module.
First I declare my objects:
>>> class Fruits:pass ... >>> banana = Fruits() >>> banana.color = 'yellow' >>> banana.value = 30
After that, I open a file called "Fruits.obj" (I previously created a new .txt file and renamed "Fruits.obj"):
>>> import pickle >>> filehandler = open(b"Fruits.obj","wb") >>> pickle.dump(banana,filehandler)
After that, I will close the session, and I started a new one, and I set the following (attempt to access the object that he had to save):
file = open("Fruits.obj",'r') object_file = pickle.load(file)
But I have this message:
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Python31\lib\pickle.py", line 1365, in load encoding=encoding, errors=errors).load() ValueError: read() from the underlying stream did notreturn bytes
I do not know what to do, because I do not understand this message. Does anyone know how I can upload my banana object? Thank!
EDIT: As I said, I said:
>>> import pickle >>> file = open("Fruits.obj",'rb')
There were no problems, but the following question:
>>> object_file = pickle.load(file)
And I have an error:
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Python31\lib\pickle.py", line 1365, in load encoding=encoding, errors=errors).load() EOFError