Python File seek() Method

Example

Change the current file position to 4 and return the rest of the lines:

f = open("demofile.txt", "r")
f.seek(4)
print(f.readline())

Run Instance

Definition and Usage

The seek() method sets the current file position in the file stream.

The seek() method also returns the new position.

Syntax

file.seek(Offset)

Parameter Value

Parameter Description
Offset Required. Numeric value, indicating the position to set the current file stream position.

More Examples

Example

Return new position:

f = open("demofile.txt", "r")
print(f.seek(4))

Run Instance