seek() and tell() in python class 12 | difference between seek and tell in python with example in Python 2023

seek() and tell() in python 

Data file handling Within a file, we can get the current position of cursor and move the cursor. To do this python gives us two functions, tell()and seek().

Highlights 

  • tell() method
  • seek() method
  • Example Program 

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()

seek() and tell() in python class 12

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 2-2 below demonstrates the usage of seek() and tell().

offset - Required parameter. Sets the cursor to the specified position and starts reading after that position. whence - Optional parameter. It is used to set the point of reference to start from which place.

  •  0 - Default. Sets the point of reference at the beginning of the file. Equivalent to os.SEEK_SET.
  •  1 - Sets the point of reference at the current position of the file. Equivalent to os.SEEK_CUR.
  •  2 - Sets the point of reference at the end of the file. Equivalent to os.SEEK_END.

 

Also check Binary File Handling in Python class 12 cbse | Binary File | dump() and load() | Pickling and un pickling 2023


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 2-2:

>>> 

 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]


Example 2: 

fh = open ("text.txt", "r+")  

print ("Name of the file: ", fh.name)  

line_1 = fh.readline()  

print ("Read Line: %s" % (line_1))  

  # Again, set the pointer to the beginning  

fh.seek (0, 1)  

line_2 = fh.readline()  

print ("Read Line: %s" % (line_2))  

# Close opend file  

fh.close()


Also Check Simple way to Read a text file line by line and display each word separated by a # | Python Class 12 computer science 083 new   


Related Tags

  • seek() function in python example
  • tell() in python
  • difference between seek and tell in python
  • difference between seek and tell in python with example
  • seek and tell in python class 12
  • seek in python w3schools
  • seek(n 1) function in python
  • write the use of seek & tell function.
  • difference between seek and tell in python
  • seek() function in python example
  • difference between seek and tell in python with example
  • seek() function in python
  • tell() function in python
  • what is the use of seek() method in files
  • csv file in python cbse class 12
  • seek(n 1) function in python

Post a Comment

If you any doubts, Please let me know

Previous Post Next Post