I’m using the latest python release and after searching, I can’t seem to find anything on pickles that will work for me.
I am simply going through tutorials attempting to learn about pickling and none of the source code that apparently works on the tutorials will work for me, I suspect this is something to do with the tutorials being outdated.
What I have tried and is the same as what tutorials show is:
import pickle
lists = [1,2,3,4,5]
pickle.dump(lists, open('log.txt', 'a+'))
which gives me the following error:
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
pickle.dump(lists, open('log.txt', 'a+'))
TypeError: must be str, not bytes
this
>>> import pickle
>>> unpicklefile = open('log.txt', 'r')
>>> unpickledlist = [1,2,3,4,5]
>>> unpickledlist = pickle.load(unpicklefile)
gives me the following error:
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
unpickledlist = pickle.load(unpicklefile)
TypeError: 'str' does not support the buffer interface
Thank you for any replies and help
The
'a+'mode may be causing you problems. And, if you’re on Windows, it would be useful to open a file in a binary mode. Also, you should close the file before reopening to read it back in. And make sure you’re writing and reading the same file (‘log.txt’ vs. ‘filename’):