Python file.tell() Method

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

Definition

tell() reports where you are in a file: the position the next read or write will use. In binary mode it is simply the number of bytes from the start.

In text mode the number is officially an opaque cookie: something you save and later pass to seek() to come back. For simple UTF-8 text it happens to equal the byte offset, but it never counts characters.

file.tell() 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

Call it with no arguments, usually to save a position for later.

python
position = f.tell()
f.seek(position)
CallWhat it does
f.tell()Right after open(path, "rb"): 0.
f.tell()After f.read(8) in binary mode: 8.
f.seek(f.tell())Save and restore a position; the safe pattern in text mode.

While a for line in f: loop is running over a text file, tell() raises OSError; use readline() in a while loop if you need positions per line.

Method signatures
python
file.tell()   # -> int
# binary files: byte offset from the start
# text files: opaque number; only pass it back to seek()

Parameters

tell() takes no parameters.

Return Value

Return ValueDescription
intBinary and raw files: the byte offset from the start of the file.
int (cookie)Text files: a value to give back to seek(). Do not do arithmetic with it.

Python Examples

Each example works inside a temporary folder that is deleted at the end, so nothing is left on your disk.

Example 1 — Report How Far a Parser Has Read

Read a 40-byte binary file in steps and turn tell() into a percentage.

python
import os
import tempfile

with tempfile.TemporaryDirectory() as folder:
    path = os.path.join(folder, "frames.bin")
    with open(path, "wb") as f:
        f.write(bytes(40))

    size = os.path.getsize(path)
    with open(path, "rb") as f:
        print(f.tell())
        for step in (8, 12, 100):
            f.read(step)
            print(f"{f.tell()} / {size} = {f.tell() * 100 // size}%")

How It Works

  1. Start at zero. A freshly opened binary file reports position 0.
  2. Position adds up. Reading 8 bytes moves to 8, then 12 more moves to 20.
  3. Capped at the end. Asking for 100 bytes only reads the 20 that remain, so the position stops at 40 and the report shows 100%.

Example 2 — Build a Byte-Offset Index of Lines

Record where each chapter title starts, then jump straight to the third one.

python
import os
import tempfile

with tempfile.TemporaryDirectory() as folder:
    path = os.path.join(folder, "chapters.txt")
    with open(path, "wb") as f:
        f.write(b"Intro\nThe Storm\nHarbor\nEpilogue\n")

    offsets = []
    with open(path, "rb") as f:
        while True:
            start = f.tell()
            if not f.readline():
                break
            offsets.append(start)
        print(offsets)
        f.seek(offsets[2])
        print(f.readline())

How It Works

  1. Position before each line. tell() is called before readline(), so each saved number is where a line begins.
  2. Byte arithmetic. Intro\n is 6 bytes and The Storm\n is 10, giving offsets 0, 6, and 16.
  3. Direct access. seek(offsets[2]) jumps to the third chapter without reading the ones before it.
  4. Why binary. In "rb" mode offsets are real byte counts on every OS, so the index could even be saved and reused.

Frequently Asked Questions

The current position. For binary files it is the byte offset from the start. For text files it is an opaque number that you should only pass back to seek().
No. It is based on bytes, so accented letters, emoji, and Windows "\r\n" line endings make it larger than the character count.
You called it inside a for line in f loop over a text file. Read with readline() in a while loop instead when you need positions.
Save pos = f.tell(), read on, then call f.seek(pos). This works in both text and binary mode.
It is the size of the file, because append mode starts at the end.

Next: file.truncate()

Resize a file to the current position or to a given size, without moving the position.

truncate() →

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.

16 people found this page helpful