简体   繁体   中英

How to specify text mode in Python's tempfile.TemporaryFile()?

In the documentation , TemporaryFile() mentions

The returned object is a file-like object whose _file attribute is either an io.BytesIO or io.StringIO object (depending on whether binary or text mode was specified)...

However, as of Python 3.6, there is no text=True method of asking for text mode. How does one do it?

TemporaryFile signature:

 tempfile.TemporaryFile(mode='w+b', buffering=None, encoding=None, newline=None, suffix=None, prefix=None, dir=None)

We are interested in a mode parameter. The last symbol is 'b', which means binary mode. If you pass mode='wt' , it will be opened in a text mode.

All modes are described on this page: https://docs.python.org/3/library/functions.html#open

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM