Home >>Python File Methods >Python File seek() Method

Python File seek() Method

Python File seek() Method

The seek() method is an inbuilt method in python, and is used to set the current file position at the offset handle to a given specific position. This method helps to return the new position.

Syntax:
file.seek(offset)

Parameter Values

Parameter Description
offset It is Required and is used to set the current file position.
Here is an example of Python File seek() Method:

z = open("demofile.txt", "r")
z.seek(2)
print(z.readline())

Output:
o! Welcome to demofile.txt
Example 2:

x = open("demofile1.txt", "r")
x.seek(4)
print(x.readline())

Output:
o! Welcome to demofile.txt

No Sidebar ads