file.seek() Move file position
Last updated: 2026-09-22
file.seek() Move file position
file.seek(offset, whence=0) moves the file position; whence 0=start, 1=current, 2=end.
| Category | File Methods |
|---|---|
| Kind | Built-in Type Method |
| Python Version | all |
📝 Syntax
file.seek()
⚙️ Parameters
| offset Required | Offset relative to whence; may be negative. |
|---|---|
| whence Optional | Reference: 0=start, 1=current position, 2=end.(Default:0) |
Returns:int. The new file position.
Mutates the original:No (returns a new object)
💥 Raises
OSError— Raised when the file is not seekable or the operation fails.
▶ Example
Edit the code, press Run, and see the result in the output panel:
Output:
34
💡 Related Cases
More case studies coming soon — check back later.
❓ FAQ
QHow do I understand the whence parameter of seek()?
A0 means relative to the beginning of the file, 1 relative to the current position, and 2 relative to the end. offset can be negative (moving backward from the reference).
QWhat limitations apply in text mode?
AIn text mode you can only position from the start (whence=0), and offset generally can only use the value returned by tell(); non-zero offsets from the current position or the end raise OSError. Binary mode supports whence=1/2.
QHow do I know the position after seek()?
Aseek() returns the new position after moving, and you can also query it at any time with tell().
QHow do I return to the start to reread?
Af.seek(0).