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.

CategoryFile Methods
KindBuilt-in Type Method
Python Versionall

📝 Syntax

file.seek()

⚙️ Parameters

offset RequiredOffset relative to whence; may be negative.
whence OptionalReference: 0=start, 1=current position, 2=end.(Default:0)

Returns:int. The new file position.

Mutates the original:No (returns a new object)

💥 Raises

▶ Example

Edit the code, press Run, and see the result in the output panel:

Output:

34
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).
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏