Python tell function
last modified November 29, 2026 Python 3.15
In this article we show how to use the tell method to read the current file position. We look at the position after reads and writes, the opaque cookie of text mode, the byte offset of binary mode, and saving a position for a later seek.
Moving the position is covered in Python seek function. Reading is explained in Python read function and Python readline function, and writing in Python write function.
The text file
The examples use the small file of city names, saved in UTF-8 with one name per line and a trailing newline.
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.
Position after a read
tell returns a number that describes where the next read will start. On a freshly opened file it is 0, and each read advances it.
with open('cities.txt', encoding='utf-8') as f:
print(f.tell())
print(repr(f.read(5)))
print(f.tell())
print(repr(f.read(1)))
print(f.tell())
The position starts at 0, becomes 5 after Paris, and 6 after the newline. In this ASCII-only part of the file the number also equals the character count.
$ python tell_read.py 0 'Paris' 5 '\n' 6
Text mode cookies
In text mode the number is an opaque cookie, not a character count. Reading the six characters of Kraków moves the cookie by seven, because the word occupies seven bytes.
with open('cities.txt', encoding='utf-8') as f:
f.read(20)
print(f.tell())
word = f.read(6)
print(word, f.tell(), len(word))
After 20 ASCII characters the position is 20. After Kraków it is 27, while the word has 6 characters. In this UTF-8 file the cookie happens to equal the byte offset, but the documentation only guarantees that seek accepts it; other encodings can encode decoder state in the number.
$ python tell_text.py 20 Kraków 27 6
Binary mode
In binary mode tell returns the byte offset exactly, with no translation and no decoding.
with open('cities.txt', 'rb') as f:
f.read(5)
print(f.tell())
data = f.read(8)
print(data, f.tell(), len(data))
Five bytes are read, then eight more, so the position is 13 and matches len(data). The data starts with the newline that ended the first line, which is why the second line is eight bytes long.
$ python tell_binary.py 5 b'\nPrague\n' 13 8
After readline
readline leaves the position at the start of the next line, which makes tell a natural marker between records.
with open('cities.txt', encoding='utf-8') as f:
print(f.readline().rstrip())
mark = f.tell()
print(f.readline().rstrip())
f.seek(mark)
print(f.readline().rstrip())
print(f.tell())
Paris is printed, the mark is 6, Prague is printed, and seeking back to 6 reads Prague again. The final call reports 13, where Vienna begins. The mark is a number to be stored, not a line index.
$ python tell_line.py Paris Prague Prague 13
Write mode
Writes advance the position as well. In text mode the value of tell is still measured in bytes, so it can grow faster than the number of characters written.
with open('notes.txt', 'w', encoding='utf-8') as f:
print(f.tell())
print(f.write('milk\n'))
print(f.tell())
print(f.write('Kraków\n'))
print(f.tell())
The position starts at 0, is 5 after milk\n, and 13 after Kraków\n. The two writes return 5 and 7 characters, but Kraków takes eight bytes. The with block flushes the data at the end; tell reports the logical position during the block.
$ python tell_write.py 0 5 5 7 13
File size
Seeking to the end and calling tell gives the size of the file in bytes, which differs from the number of characters when the text is not pure ASCII.
with open('cities.txt', encoding='utf-8') as f:
f.seek(0, 2)
print(f.tell())
f.seek(0)
print(len(f.read()))
The file is 44 bytes but 43 characters, and reading it back confirms the character count. In binary mode the same two numbers would be equal.
$ python tell_size.py 44 43
Resuming at a marker
Because the position can be stored in a variable, a scan can stop at a record, note where it ends, and return to it later.
with open('cities.txt', encoding='utf-8') as f:
while line := f.readline():
if line.startswith('B'):
mark = f.tell()
print(line.rstrip(), mark)
f.seek(mark)
print(f.readline().rstrip())
The loop reads until Budapest, prints the word with the position 37, and reaches the end of the file. Seeking back to 37 and reading one line returns Warsaw. The mark is 37 rather than 36 because Kraków added one extra byte.
$ python tell_resume.py Budapest 37 Warsaw
Source
IOBase.tell – Python documentation
Methods of File Objects – Python Tutorial
In this article we have read the position after reads, writes, and lines, compared the text cookie with the binary byte offset, and used a stored position to resume reading. See also Python seek function, Python readline function, and Python read function.
Author
List all Python tutorials.