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
Comments