top of page
Writer's pictureRajesh Singh

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



54 views0 comments

Recent Posts

See All

Files MCQ in Python

File Processing MCQ Q1. Which function writes a list of lines in File? A.      Writelines() B.      Write() C.      Tell() D.      All of...

Python List MCQ Questions

Q1. What is output of below python code? dict= {1:'1', 2:'2', 3:'3'} del dict[1] dict[1] = '10' del dict[2] print (len(dict)) A. 1 B. 2...

Python NUMPY MCQ Questions

Python NumPy Chapter MCQ: 1. What will be print? Import numpy as np a = np. array([1,2,3,5,8]) b = np. array([0,3,4,2,1]) c = a+b c = c*a...

Comments


bottom of page