classification
Title: json.dump: fp must be a text file object
Type: Stage: resolved
Components: Library (Lib) Versions: Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: berker.peksag, qingyunha
Priority: normal Keywords:

Created on 2017-12-18 06:24 by qingyunha, last changed 2018-03-23 21:27 by berker.peksag. This issue is now closed.

Messages (2)
msg308517 - (view) Author: TaoQingyun (qingyunha) * Date: 2017-12-18 06:24
```
>>> 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 may not a bug. But it should mention at docs https://docs.python.org/3/library/json.html#json.dump
msg314332 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2018-03-23 21:27
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'
History
Date User Action Args
2018-03-23 21:27:27berker.peksagsetstatus: open -> closed

nosy: + berker.peksag
messages: + msg314332

resolution: not a bug
stage: resolved
2017-12-18 06:24:46qingyunhacreate