The TextIOWrapper class in Python

The class TextIOWrapper from the io module of Python Standard Library is a text input-output stream that provides encoding and decoding over an underlying buffer.

Textiowrapper Updated 2 Nov 2025 1 min read

Overview

The TextIOWrapper is an input and output stream that specializes in handling text encoding and decoding while performing write and read operations. The stream supports both input and output operations.

TextIOWrapper supports buffered input and output. Buffering refers to the technique of gathering some amount of bytes before writing it to the destination. TextIOWrapper gathers the contents up to the occurrence of a new line character before making a call to flush. Buffering can be turned-off if write_through is enabled.

The example uses a BytesIO object as the underlying buffer.

Example

python
# Example Python program that writes to and

            # read from a buffer using the TextIOWrapper.

            # The buffer used is a BytesIO object.

            import io

# Create a buffer

            buffer = io.BytesIO()

# Create a stream that handles text

            textStream = io.TextIOWrapper(buffer, encoding='utf-8', line_buffering = True)

# Write to the TextIOWrapper and flush it when done

            textStream.write("\nHello\n")

            textStream.write("\nWorld\n")

            textStream.flush()

# While writing the file pointer moved...

            # Reset it to the beginning of the file

            textStream.seek(0)

# Read from TextIOWrapper using iterator protocol

            for line in textStream:

                print(line)

print("Encoding used:")

            print(textStream.encoding)

print("Write_through:")

            print(textStream.write_through)

print("Line buffering:")

            print(textStream.line_buffering)

print("Default buffer size:")

            print(io.DEFAULT_BUFFER_SIZE)

Output

Output

Hello

World

Encoding used:

            utf-8

            Write_through:

            False

            Line buffering:

            True

            Default buffer size:

            8192