You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2018-03-23.21:27:27.992>created_at=<Date2017-12-18.06:24:46.414>labels= ['invalid', 'library']
title='json.dump: fp must be a text file object'updated_at=<Date2018-03-23.21:27:27.981>user='https://github.com/qingyunha'
>>> import json
>>> f = open('/tmp/t.json', 'wb')
>>> json.dump(123, f)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.6/json/__init__.py", line 180, in dump
fp.write(chunk)
TypeError: a bytes-like object is required, not 'str'
This is already documented in the json.dump() documentation:
The json module always produces str objects, not bytes objects.
Therefore, fp.write() must support str input.
Note that the traceback you've posted doesn't have anything to do with the json module and it's expected:
>>> f = open('/tmp/t.json', 'wb')
>>> f.write('foo')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: a bytes-like object is required, not 'str'
Activity
qingyunha commentedon Dec 18, 2017
This may not a bug. But it should mention at docs https://docs.python.org/3/library/json.html#json.dump
berkerpeksag commentedon Mar 23, 2018
This is already documented in the json.dump() documentation:
Note that the traceback you've posted doesn't have anything to do with the json module and it's expected:
>>> f = open('/tmp/t.json', 'wb') >>> f.write('foo') Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: a bytes-like object is required, not 'str'