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.
# 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
Stream pointer at position:44
Writing more text...into the stream