Python Glossary Русский

all Python 3codecsterm

codecs.StreamReaderWriter

stream codecs

A wrapper that combines a StreamReader and a StreamWriter over one stream, so it both reads and writes text. It is what codecs.open returns.

Syntax

codecs.StreamReaderWriter(stream, Reader, Writer, errors='strict')

If you meet one in a debugger, the file was opened with the deprecated codecs.open, not open. It behaves differently from io.TextIOWrapper: no newline translation, and seek resets the codec state. Building one by hand is rare: it needs a stream, a Reader class and a Writer class.

Examples

import codecs, io
f = codecs.StreamReaderWriter(io.BytesIO(), codecs.getreader('utf-8'), codecs.getwriter('utf-8'))
f.write('ёж')
print(f.stream.getvalue())  # → b'\xd1\x91\xd0\xb6'
f.seek(0)
print(f.read())  # → ёж

See also

Open in the full glossarydocs.python.orgReport an inaccuracy