TypeError: an integer is required (obtained of type _io.BufferedWriter) using pickle

the code:

import pickle
test = 3

>>> with open('test', 'wb') as file:
...     pickle.dumps(test, file)

and the error message appeared unexpectedly.

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
TypeError: an integer is required (got type _io.BufferedWriter)

What's going on here?

+7
source share
1 answer

You are using the wrong function. Here are the docs:

dumps(obj, protocol=None, *, fix_imports=True)

Return the marinated representation of an object as an object bytes.

dumpsconverts the passed object to bytesand returns it. An error occurs when you pass a file argument to what it .dumpexpects to be an etching protocol, which must be an integer.

You want to use pickle.dumpthat is actually uploaded to a file:

dump(obj, file, protocol=None, *, fix_imports=True)

obj file.

with open('test', 'wb') as file:
    pickle.dump(test, file)
+9

Source: https://habr.com/ru/post/1684856/


All Articles