Python Glossary Русский

all Python 3Files & I/Ofunction

io.IOBase.tell()

file position

Returns the current position in the file: a byte offset in binary mode, an opaque number meant for seek() in text mode.

Syntax

file.tell() -> int

In binary mode tell() gives a genuine byte offset; in text mode it returns an opaque cookie you must not do arithmetic on — the only legal use is handing it back to seek(). If you need to compute positions or jump N bytes ahead, open the file with 'b'. Text mode also translates line endings, so the number from tell() will not match how many characters you have read.

Examples

with open('data.txt', 'wb') as f:
    f.write(b'hello world')
with open('data.txt', 'rb') as f:
    f.read(5)
    print(f.tell())     # → 5
    f.read(3)
    print(f.tell())     # → 8
    f.seek(0)           # вернуться в начало
    print(f.tell())     # → 0

See also

Open in the full glossarydocs.python.orgReport an inaccuracy