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)
Call
What 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 Value
Description
int
Binary 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}%")
Output
0
8 / 40 = 20%
20 / 40 = 50%
40 / 40 = 100%
How It Works
Start at zero. A freshly opened binary file reports position 0.
Position adds up. Reading 8 bytes moves to 8, then 12 more moves to 20.
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())
Output
[0, 6, 16, 23]
b'Harbor\n'
How It Works
Position before each line.tell() is called before readline(), so each saved number is where a line begins.
Byte arithmetic.Intro\n is 6 bytes and The Storm\n is 10, giving offsets 0, 6, and 16.
Direct access.seek(offsets[2]) jumps to the third chapter without reading the ones before it.
Why binary. In "rb" mode offsets are real byte counts on every OS, so the index could even be saved and reused.
Example 3 — See tell() Count Bytes in a Text File
Write ASCII and accented text, then reopen in append mode and check where it starts.
python
import os
import tempfile
with tempfile.TemporaryDirectory() as folder:
path = os.path.join(folder, "diary.txt")
with open(path, "w", encoding="utf-8", newline="\n") as f:
f.write("Hi\n")
print("after 'Hi\\n':", f.tell())
f.write("caf\u00e9\n")
print("after 5 more characters:", f.tell())
with open(path, "a", encoding="utf-8", newline="\n") as f:
print("append mode starts at:", f.tell())
Output
after 'Hi\n': 3
after 5 more characters: 9
append mode starts at: 9
How It Works
Simple ASCII text.Hi\n is 3 characters and 3 bytes, so tell() reports 3.
Bytes, not characters.café\n is 5 characters but 6 UTF-8 bytes, so CPython reports 9, not 8.
Append starts at the end. Reopening with "a" puts the position at the current size, 9.
Stable newlines.newline="\n" keeps each line ending one byte long; without it, Windows would report larger numbers.
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.