top of page

Seek function in python with example

  • Writer: Rajesh Singh
    Rajesh Singh
  • Feb 19, 2022
  • 1 min read


Seek () function is used to change the position of the file handle to a given specific position in python. File handle such as a cursor which defines from where the data has to be read or written in the file. In other words we can say that seek () function sets the present file position in a file stream. Seek () function also returns the new position. Seek () does not return any value in Python.

Syntax: fileobj.seek (offset)

“Offset is necessary a number representing the situation to set the present file stream position.”

Example:

fileobj=open (“careerbodh.txt”, “r”)

print(“File Position: ”,fileobj.tell())

print(fileobj.readline())

fileobj.seek(5)

print(fileobj.readline())

print(“File Position: “,fileobj.seek(5))

fileobj.close ()

Output:

File Position: 0

Career Bodh Sansthan

Sansthan

File Position: 5



Recent Posts

See All
For Loop MCQ in Python

Q1. What is the output of the following Python code? x={1:"X",2:"Y",3:"Z"} for i,j in x.items():     print(i,j,end=" ") A.    1 X 2 Y 3 Z...

 
 
 

Kommentare


bottom of page