Usage of pickle.dump in Python
The problem is that you're opening the file in text mode. You need to use binary here:
>>> f = open('data.txt','w')>>> pickle.dump(123,f)Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: must be str, not bytes>>> >>> f = open('data.txt','wb')>>> pickle.dump(123,f)>>>
The write method for file-like objects, only accept a single string argument. The dumps method in the pickle module automatically casts arguments as strings, whereas the the dump method will write a pickled representation of the object to the open file. Since 123 is not a string it throws the TypeError error.
This is acknowledged in pickle.dump documentation.