1
with open('data', 'w') as f:
    pickle.dumps({'foo':111},f)

results in

an integer is required (got type _io.TextIOWrapper)

How can I fix this?

I am pretty sure An integer is required? open() was not called beforehand. Python version is 3.6.2

Georg Heiler
  • 16,916
  • 36
  • 162
  • 292
  • 1
    [Second argument to `dumps` is _`protocol`_](https://docs.python.org/3/library/pickle.html#pickle.dumps). Did you mean `pickle.dumps({'foo':111},f)`? – Łukasz Rogalski Aug 16 '17 at 14:06

1 Answers1

1

pickle.dumps dumps obj into a string which it returns. In order to write into a file, you probably want to use pickle.dump (without the s).

with open('data', 'wb') as f:
    pickle.dump({'foo':111}, f)

Additionally you should also open the file in binary mode, because pickle.dump will write binary data.

Stefan Kögl
  • 4,383
  • 1
  • 27
  • 34