Every open file has a position: the place where the next read or write happens. seek() moves it, and returns the new position counted from the start of the file.
In binary mode you can jump anywhere: from the start, relative to where you are, or back from the end. In text mode only safe jumps are allowed: to the start, to the end, or to a value you saved from tell().
file.seek() is a method of the file objects that the built-in open() returns, so no import is needed. It works on text files (TextIOWrapper), binary files (BufferedReader, BufferedWriter, BufferedRandom), and raw files (FileIO, opened with buffering=0).
Syntax
Pass an offset, and optionally a whence that says what the offset is measured from.
python
f.seek(offset)
f.seek(offset, whence)
Call
What it does
f.seek(0)
Rewind to the start; works in every mode.
f.seek(-8, os.SEEK_END)
Binary mode: 8 bytes before the end.
f.seek(0, os.SEEK_END)
Jump to the end, for example before measuring or appending.
os.SEEK_SET, os.SEEK_CUR, and os.SEEK_END are the readable names for 0, 1, and 2 (also available as io.SEEK_SET and friends).
Method signatures
python
file.seek(offset, whence=os.SEEK_SET, /) # -> int, the new absolute position
# whence: 0 = SEEK_SET (start), 1 = SEEK_CUR (current), 2 = SEEK_END (end)
# text files: offset must be 0 or a value from tell(); only seek(0, 2) at the end
Parameters
Parameter
Description
offset
Binary mode: a byte count, which may be negative with SEEK_CUR or SEEK_END. Text mode: 0 or a number returned by tell().
whence
Optional. 0 (default) from the start, 1 from the current position, 2 from the end.
Return Value
Return Value
Description
int
The new position measured from the start of the file. In text mode treat it as an opaque value.
Python Examples
Each example works inside a temporary folder that is deleted at the end, so nothing is left on your disk.
Example 1 — Jump Between Fixed-Size Records
A binary file of 4-byte records: jump from the start, relative to the current position, and back from the end.
python
import os
import tempfile
with tempfile.TemporaryDirectory() as folder:
path = os.path.join(folder, "names.dat")
with open(path, "wb") as f:
f.write(b"ID01MinaKofiLars")
with open(path, "rb") as f:
print(f.seek(4), f.read(4))
print(f.seek(4, os.SEEK_CUR), f.read(4))
print(f.seek(-8, os.SEEK_END), f.read(4))
Output
4 b'Mina'
12 b'Lars'
8 b'Kofi'
How It Works
Record layout. Every record is 4 bytes, so record n starts at byte 4 * n.
From the start.seek(4) skips the ID record, returns 4, and the next read gives Mina.
Relative jump. After that read the position is 8; seek(4, os.SEEK_CUR) skips Kofi and lands on 12.
From the end. The file is 16 bytes, so seek(-8, os.SEEK_END) lands on 8, the start of Kofi.
Example 2 — Rewind a Text File and Hit the Relative-Seek Rule
Count guests, rewind for the first name, then try a relative move that text mode forbids.
python
import io
import os
import tempfile
with tempfile.TemporaryDirectory() as folder:
path = os.path.join(folder, "guests.txt")
with open(path, "w", encoding="utf-8") as f:
f.write("Ines\nJoao\nKemal\n")
with open(path, encoding="utf-8") as f:
total = len(f.readlines())
f.seek(0)
print(total, "guests, first:", f.readline().strip())
try:
f.seek(2, os.SEEK_CUR)
except io.UnsupportedOperation:
print("text files allow only seek(0, 1) relative moves")
print(f.seek(0, os.SEEK_END) == os.path.getsize(path))
Output
3 guests, first: Ines
text files allow only seek(0, 1) relative moves
True
How It Works
Read twice.readlines() leaves the position at the end; seek(0) rewinds so readline() can return the first guest.
Forbidden move. A non-zero SEEK_CUR offset raises io.UnsupportedOperation in text mode.
End is allowed.seek(0, os.SEEK_END) works and returns the byte size of the file, including any \r\n on Windows, so it equals os.path.getsize() everywhere.
Example 3 — Seek Past the End and Write a Byte
Jump beyond the last byte of a "w+b" file and see how the gap is filled.
python
import os
import tempfile
with tempfile.TemporaryDirectory() as folder:
path = os.path.join(folder, "sparse.bin")
with open(path, "w+b") as f:
f.write(b"AB")
print(f.seek(5))
f.write(b"Z")
f.seek(0)
data = f.read()
print(data, len(data))
Output
5
b'AB\x00\x00\x00Z' 6
The gap reads back as zero bytes on common file systems (ext4, APFS, NTFS); some file systems store it as a sparse hole.
How It Works
Beyond the data. The file holds 2 bytes, yet seek(5) succeeds and returns 5.
Write extends the file. Writing Z at offset 5 makes the file 6 bytes long.
Zero-filled gap. Offsets 2 to 4 were never written, so they read back as \x00 bytes.
Frequently Asked Questions
Call f.seek(0). It works in text and binary mode, and the next read starts from the first character again.
It says where the offset is measured from: 0 (os.SEEK_SET) the start, 1 (os.SEEK_CUR) the current position, 2 (os.SEEK_END) the end of the file.
Text files only allow seek(0, 1), seek(0, 2), and absolute positions saved from tell(), because characters can be several bytes long. Open the file in binary mode for arbitrary moves.
The new absolute position counted from the start of the file. In text mode treat it as an opaque value that you can pass back to seek().
Yes, in binary mode. Reading there returns empty bytes; writing there extends the file and the gap reads back as zero bytes.