Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
community creator

How to use seek() and tell() in Python

Yamini Gujjidi

tell()

tell() returns the current position of the file pointer from the beginning of the file.

main.py
demofile.txt
f = open("demofile.txt", "r")
# points at the start
print(f.tell())

seek()

If we want to move the file pointer to another position, we can use the seek() method.

Syntax

This method takes two arguments:

  • f.seek(offset, fromwhere), where offset represents how many bytes to move
  • fromwhere, represents the position from where the bytes are moving.
main.py
demofile.txt
f = open("demofile.txt", "r")
f.seek(4)
print(f.readline())

RELATED TAGS

python
community creator
RELATED COURSES

View all Courses

Keep Exploring