all Python 3ioterm
io.TextIOWrapper
wrapper
Wraps a binary stream and adds text encoding and decoding. It is what open() uses internally.
Syntax
io.TextIOWrapper(buffer, encoding=None, errors=None, newline=None)
Closing the wrapper also closes the binary stream underneath, so call detach() first if you still need that stream. Two things the signature does not show: with newline=None a written newline is translated to os.linesep (CR LF on Windows), and seek() on a text stream accepts only the opaque values previously returned by tell(), not arbitrary byte offsets.
Examples
import io
raw = io.BytesIO(b'hello')
wrapper = io.TextIOWrapper(raw, encoding='utf-8')
wrapper.read() # → 'hello'
import io
# Запись:
buf = io.BytesIO()
tw = io.TextIOWrapper(buf, encoding='utf-8')
tw.write('мир')
tw.flush()
buf.getvalue() # → b'\xd0\xbc\xd0\xb8\xd1\x80'
See also
Open in the full glossarydocs.python.orgReport an inaccuracy