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

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())

Free Resources