Use app×
QUIZARD
QUIZARD
JEE MAIN 2026 Crash Course
NEET 2026 Crash Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
542 views
in Computer by (49.6k points)
closed by

Setting Offsets in a File.

1 Answer

+1 vote
by (51.9k points)
selected by
 
Best answer

The functions that we have learnt till now are used to access the data sequentially from a file. But if we want to access data in a random fashion, then Python gives us seek() and tell() functions to do so.

The tell() method

This function returns an integer that specifies the current position of the file object in the file. The position so specified is the byte position from the beginning of the file till the current position of the file object. 

The syntax of using tell() is:

file_object.tell()

The seek() method

This method is used to position the file object at a particular position in a file.

The syntax of seek() is:

file_object.seek(offset [, reference_point])

In the above syntax, offset is the number of bytes by which the file object is to be moved. reference_point indicates the starting position of the file object. That is, with reference to which position, the offset has to be counted. 

It can have any of the following values: 

  • 0 - beginning of the file 
  • 1 - current position of the file 
  • 2 - end of file 

By default, the value of reference_point is 0, i.e. the offset is counted from the beginning of the file. For example, the statement fileObject.seek(5,0) will position the file object at 5th byte position from the beginning of the file. The code in Program below demonstrates the usage of seek() and tell().

Program: Application of seek() and tell()

print("Learning to move the file object") 

fileobject=open("testfile.txt","r+") 

str=fileobject.read() print(str) 

print("Initially, the position of the file object is: ",fileobject. 

tell()) 

fileobject.seek(0) 

print("Now the file object is at the beginning of the file: 

",fileobject.tell()) 

fileobject.seek(10) 

print("We are moving to 10th byte position from the beginning of 

file") 

print("The position of the file object is at", fileobject.tell()) 

str=fileobject.read() 

print(str)

Output of Program :

>>> 

RESTART: Path_to_file\Program2-2.py 

Learning to move the file object 

roll_numbers = [1, 2, 3, 4, 5, 6] 

Initially, the position of the file object is: 33 

Now the file object is at the beginning of the file: 0 

We are moving to 10th byte position from the beginning of file 

The position of the file object is at 10 

numbers = [1, 2, 3, 4, 5, 6] 

>>>

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...