top of page

Seek function in python with example



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



28 views0 comments

Recent Posts

See All

Tell () function can be used to get the position of file handle and returns current position of file object where as Seek () function is used to change the position of the file handle to a given speci

Slicing is a Python attitude that enables accessing parts of data from the given sequence like list, tuples etc. A slice object is created based on a set of indices. Slicing starts anywhere from list

bottom of page