This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Can't write to NamedTemporaryFile
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.6
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Kevin Bonham, r.david.murray
Priority: normal Keywords:

Created on 2017-01-11 19:44 by Kevin Bonham, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (2)
msg285266 - (view) Author: Kevin Bonham (Kevin Bonham) Date: 2017-01-11 19:44
Python 3.6.0 (default, Dec 24 2016, 08:01:42) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from tempfile import NamedTemporaryFile
>>> tmp = NamedTemporaryFile()
>>> tmp.write("hello world!")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/Cellar/python3/3.6.0/Frameworks/Python.framework/Versions/3.6/lib/python3.6/", line 483, in func_wrapper
    return func(*args, **kwargs)
TypeError: a bytes-like object is required, not 'str'
>>> type(tmp)
<class 'tempfile._TemporaryFileWrapper'>

The more verbose error points to issue #18879 (, which seems very similar to my problem, but is marked as resolved.

TypeError                                 Traceback (most recent call last)
<ipython-input-23-b9a4238a79b7> in <module>()
----> 1 tmp_file.write("blah")

/usr/local/Cellar/python3/3.6.0/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ in func_wrapper(*args, **kwargs)
    481             @_functools.wraps(func)
    482             def func_wrapper(*args, **kwargs):
--> 483                 return func(*args, **kwargs)
    484             # Avoid closing the file as long as the wrapper is alive,
    485             # see issue #18879.

TypeError: a bytes-like object is required, not 'str'

This also seems like it might be related to, though I'm getting the same behavior with TemporaryFile:

>>> from tempfile import TemporaryFile
>>> tmp2 = TemporaryFile()
>>> tmp2.write("Hello World")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: a bytes-like object is required, not 'str'

I can do:

>>> with open(, "w") as t:
...     t.write("Hello world!")

Is this intended behavior? The docs still say that these functions should return file-like objects.
msg285268 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2017-01-11 19:55
Yes, it is intended behavior, and it is documented.  The default mode for NamedTemporaryFile (and TemporaryFile) is shown in the docs as "w+b".

I suppose that used to be more convenient in python2 where there was almost no distinction between binary and text on unix, but I think we are unlikely to change the default at this point.
Date User Action Args
2022-04-11 14:58:41adminsetgithub: 73431
2017-01-11 19:55:09r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg285268

resolution: not a bug
stage: resolved
2017-01-11 19:44:31Kevin Bonhamcreate