i am trying persistence with pickle
import pickle
fichero = file("Ejemplo.txt", "w+")
pickle.dump("ejemplo", fichero)
print fichero.readlines()[-1]
fichero.close()
Since the result of the script does not fit, I am going to leave the photo of the result and the .txt file. as I gave him the order to read the last line because I need him to read me the last thing that was done, but instead of that he reads me other information, I would like to know what I can do or better what I am doing wrong
Try something like this to serialize an object:
Note that you cannot read the file if you open it with
'w'
(write data). You have to close it first or use another variable to open the file:The information you saved to the file using
pickle.dump
is serialized, that's why you see that weird text. If you want to retrieve it you have to usedump.load
to unrealize the object:Remember, the module
pickle
is used to serialize / deserialize objects. If you just want to write to a file you don't needpickle
:And to read it: