Python Reference ยท Files, Paths and Text I/O
Python file.seek() and tell()
Reference for seek() and tell() on Python file objects: rewind, jump, read from the end, whence values, text versus binary mode and truncate().
Every open file has a current position: the place where the next read or write happens. file.tell() tells you that position, and file.seek(offset, whence) moves it.
The most common use is f.seek(0) to read a file again from the start. In binary mode you can also jump to any byte, which is how programs read headers, fixed-size records or the end of a large file without reading everything before it.
f.seek(0) # back to the start pos = f.tell() # where am I?
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Syntax
file.seek(offset, whence=0) file.tell() file.truncate(size=None)
| Name | Description |
|---|---|
offset |
Number of bytes to move (or, in text mode, a value previously returned by tell()). |
whence |
0 (os.SEEK_SET): from the start. 1 (os.SEEK_CUR): from the current position. 2 (os.SEEK_END): from the end. |
size |
For truncate(): new length. Defaults to the current position. |
Return value: seek() returns the new absolute position. tell() returns the current position. truncate() returns the new size.
Raises: ValueError for a closed file or a negative result, OSError or io.UnsupportedOperation if the file cannot seek (pipes, terminals) or, in text mode, for a nonzero relative seek.
Usage notes
- Binary mode is byte-exact. Positions are byte offsets, and any
whenceworks. This is the mode to use for random access. - Text mode is restricted.
tell()returns an opaque number that encodes decoder state, not a character count. Pass only such saved values, or0, toseek()withwhence=0. The only other allowed form isseek(0, 2)to jump to the end.seek(-1, 2)raises an error. - Positions are shared by reading and writing. In a
r+orw+file, a read moves the same position a write uses. Seek between switching from reading to writing (and back) to avoid surprises. - Append mode ignores the position for writes. With
"a", every write goes to the end, whatever you seek to. - Writing overwrites, it does not insert. Writing in the middle replaces the bytes that are there. To insert text, read the rest, write the new text, then write the rest back, or rebuild the file.
truncate()changes the file's length, cutting data off or extending with zero bytes. It does not move the position.- Seeking past the end is allowed in binary mode; a later write fills the gap with zero bytes on most systems.
- Not every stream seeks.
f.seekable()tells you whetherseek()will work.