The seek() method of TextIOWrapper class in Python

The seek() method moves the stream pointer to a specified in a stream. The constants SEEK_SET, SEEK_END and SEEK_CUR from the Python io module are used along with the position obtained through the tell() method.

Textiowrapper Updated 3 Nov 2025 1 min read

Overview

  • The seek() method of TextIOWrapper class from the io module moves the stream pointer to a given position.

Example

The Python example below stores a position obtained through tell() method and uses it later to move the stream pointer.

python
# Example Python program that moves the

            # stream pointer of a TextIOWrapper to a

            # specified location using seek()

            import io

# Create a text stream - An io.TextIOWrapper object

            underlying = io.BytesIO()

            textStream = io.TextIOWrapper(underlying)

# Write some text

            textStream.write("The quick brown fox jumps over the lazy dog\n")

# Store the stream pointer

            streamPos = textStream.tell()

            print("Stream pointer at position:{}".format(streamPos))

# Write more text

            textStream.write("Writing more text...into the stream")

            textStream.flush()

# Read from the 2nd line with the position obtained

            # from tell() invocation

            textStream.seek(streamPos, io.SEEK_SET)

            for line in textStream:

                print(line)

Output

Output
Stream pointer at position:44

            Writing more text...into the stream