Python file.seek() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
file method

Definition

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)
CallWhat 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

ParameterDescription
offsetBinary mode: a byte count, which may be negative with SEEK_CUR or SEEK_END. Text mode: 0 or a number returned by tell().
whenceOptional. 0 (default) from the start, 1 from the current position, 2 from the end.

Return Value

Return ValueDescription
intThe 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))

How It Works

  1. Record layout. Every record is 4 bytes, so record n starts at byte 4 * n.
  2. From the start. seek(4) skips the ID record, returns 4, and the next read gives Mina.
  3. Relative jump. After that read the position is 8; seek(4, os.SEEK_CUR) skips Kofi and lands on 12.
  4. 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))

How It Works

  1. Read twice. readlines() leaves the position at the end; seek(0) rewinds so readline() can return the first guest.
  2. Forbidden move. A non-zero SEEK_CUR offset raises io.UnsupportedOperation in text mode.
  3. 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))

How It Works

  1. Beyond the data. The file holds 2 bytes, yet seek(5) succeeds and returns 5.
  2. Write extends the file. Writing Z at offset 5 makes the file 6 bytes long.
  3. 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.

Next: file.seekable()

Return True if the stream supports random access with seek() and tell().

seekable() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful