Python seek function
last modified November 29, 2026 Python 3.15
In this article we show how to use the seek method to move the position at which the next read or write happens. We look at the three reference points, byte offsets in binary mode, the restrictions of text mode, and the value seek returns.
The related method that reports the position is covered in Python tell function. Reading is explained in Python read function and Python readline function, and opening files in Python open function.
The text file
The text examples use the same small file of city names, saved in UTF-8 with one name per line. The binary examples write a few bytes of their own.
Paris Prague Vienna Kraków Budapest Warsaw
The accented ó in Kraków takes two bytes in UTF-8 but counts as a single character, which matters for every method that counts one or the other.
Rewinding to the start
seek(0) moves to the beginning in either mode and is always a valid position in a text file. The method returns the new absolute position.
with open('cities.txt', encoding='utf-8') as f:
print(f.readline().rstrip())
print(f.seek(0))
print(f.readline().rstrip())
The first line moves the position to 6, but the printed return value of seek(0) is 0. The second readline returns Paris again, which shows that the rewind worked.
$ python seek_start.py Paris 0 Paris
Byte offsets in binary mode
In binary mode the offset is an exact byte count from the start of the file. Seeking past the end is allowed; a read there returns an empty bytes object.
with open('raw.bin', 'wb') as f:
f.write(b'ABCDEFGHIJ')
with open('raw.bin', 'rb') as f:
print(f.seek(4))
print(f.read(3))
print(f.seek(20))
print(repr(f.read()))
seek(4) returns 4 and places the position on E, so read(3) returns EFG. seek(20) is beyond the ten-byte file and returns 20; the read returns b'' without an error.
$ python seek_binary.py 4 b'EFG' 20 b''
Relative seeks
A whence of 1 makes the offset relative to the current position. Negative offsets move backwards.
with open('raw.bin', 'wb') as f:
f.write(b'ABCDEFGHIJ')
with open('raw.bin', 'rb') as f:
print(f.read(2))
print(f.seek(3, 1))
print(f.read(2))
print(f.seek(-2, 1))
print(f.read(2))
Two bytes are read, then three are skipped and two more read, giving FG. Seeking back two bytes returns to the same F, and the same two bytes are read again. Each call prints the new absolute position, 5 in both cases.
$ python seek_current.py b'AB' 5 b'FG' 5 b'FG'
Seeking from the end
A whence of 2 measures from the end of the file. A negative offset positions before the end, and zero positions exactly at the end.
with open('raw.bin', 'wb') as f:
f.write(b'ABCDEFGHIJ')
with open('raw.bin', 'rb') as f:
print(f.seek(0, 2))
print(f.seek(-3, 2))
print(f.read())
The file is ten bytes long, so seek(0, 2) returns 10. seek(-3, 2) returns 7, and the read gives the last three bytes, HIJ.
$ python seek_end.py 10 7 b'HIJ'
Text mode restrictions
Text mode translates newlines and decodes bytes, so a byte offset has no fixed meaning there. Only whence 0 is accepted; relative seeks raise UnsupportedOperation.
with open('cities.txt', encoding='utf-8') as f:
for offset, whence in ((3, 1), (-1, 2)):
try:
f.seek(offset, whence)
except OSError as e:
print(type(e).__name__, e)
Both errors come from the text wrapper, which cannot map a relative offset to a byte position. Catching OSError covers them, because UnsupportedOperation is a subclass of it and of ValueError.
$ python seek_text_error.py UnsupportedOperation can't do nonzero cur-relative seeks UnsupportedOperation can't do nonzero end-relative seeks
Text mode cookies
In text mode the only portable offset is a cookie that tell returned, plus 0 for the start. CPython accepts any non-negative number from the start, but an arbitrary number may land in the middle of a multibyte character.
with open('cities.txt', encoding='utf-8') as f:
print(f.seek(0))
print(repr(f.read(5)))
print(f.seek(10))
print(repr(f.read(4)))
print(f.seekable())
seek(0) returns 0, and the first five characters are Paris. seek(10) returns 10, and read(4) takes ue\nV, the four characters after the first ten. The last line confirms that the file is seekable. Python tell function shows how to save a position and pass the cookie back.
$ python seek_text.py 0 'Paris' 10 'ue\nV' True
Reading the last line
A seek to the end combined with a negative offset reads a fixed-size tail without scanning the whole file.
with open('cities.txt', 'rb') as f:
size = f.seek(0, 2)
f.seek(-7, 2)
print(size)
print(f.read())
size is 44, the byte length of the file, and the last seven bytes are Warsaw\n. The binary handle avoids decoding. For text files the same trick works, but the result may start in the middle of a character, so decode only a part that is known to be aligned.
$ python seek_last_line.py 44 b'Warsaw\n'
Source
IOBase.seek – Python documentation
Methods of File Objects – Python Tutorial
In this article we have moved the position from the start, from the current position, and from the end, used byte offsets in binary mode, and seen which seeks text mode rejects. See also Python tell function, Python read function, Python readline function, and Python open function.
Author
List all Python tutorials.