Skip to content
File Methods

seek()

Change the file stream position to the given offset.

file.seek(offset, whence=0)

file.seek() repositions the stream pointer to offset bytes relative to whence.

whence values: 0 (beginning of file), 1 (current position), 2 (end of file).

Parameters

NameDescription
offsetNumber of bytes to move the stream pointer.
whenceReference point: 0 (start), 1 (current), 2 (end). Defaults to 0.

Returns

The new absolute byte offset position as an integer.

Try it

Run it and change it
import tempfile
with tempfile.NamedTemporaryFile(mode='w+', delete=True) as f:
    f.write('0123456789')
    f.seek(4)
    print('Read from pos 4:', f.read(3))

Worth knowing

  • Rewinding: f.seek(0) resets the pointer back to the beginning of the file, allowing you to re-read the file without re-opening it.

Related entries