Python File handling

Python seek and tell method

What is the use of the python seek and tell method?

When we open the file for any operation at that time by default file pointer is pointing to the beginning of the file. In some cases, we did not want to perform the file operation at the beginning of the file in that we need to seek a function to change the file pointer. For example, we want to read the last 30 characters from the file so in that case first, we need to change the file pointer using seek method and then perform the read operation on that. The tell method is used to get the current position of the file pointer. Before using this function we must need to open the file.

tell() method

The tell() method returns the integer value the represents the file pointer location from begging of the file. The begging index of the file starts from the 0 same as an index in the string.

Syntax

<File-Object>.tell()
  • tell() method not take any argument and return the current file position as integer value.

In this tutorial we use below give file for example mydata.txt

Sunday
 Monday
 Tuesday
 Wednesday
 Thursday
 Friday
 Saturday

How to use tell method

try:
    file = open("mydata.txt");
    print("File pointer that point to begging of the File :", file.tell() )
    #read on line from the file
    data = file.readline();
    print(data , end='');
    #get the file pointer after reading one line form the file
    print("File pointer after reading one line from the File:", file.tell() );
finally:
    file.close()
File pointer that point to begging of the File : 0
Sunday
File pointer after reading one line from the File: 8

In the above example first tell method call return the file pointer index that pointing to the begging of the file. And after read online from the file. We call the again tell method that return the value 8 means the file pointer currently located at 8 index in file.

Why this return value 8? because we read the “Sunday\n” from the file that is the 8 character we read from the file. In short, we read the data from 0 index to 7 indexes and now the file pointer is located at index 8.

Seek() method

The seek() method is used to change the current file pointer position and set the new position of the file pointer to perform the operation. The seek method is also used to skip some parts of the file. That helps us to read the file data efficiently.

Syntax

<file-object>.seek(offset,fromwhere)

Seek function change the file pointer position and return the new file pointer postion. This method take two values as parameters.

  • Offset: The offset represents the number of the position we want to seek.
  • from where: The from where we want to move the file pointer this takes an integer value as a point of reference. This is an optional argument by default it takes 0 as reference value

The allowed value for the second values is given in the below-given table. We just need to pass the value from the given table according to our requirements.

Value Description
0This value represents the reference position is starts and moves the file pointer from the starting of the file. In this case, the offset value must be positive
1This value represents the reference position is from the current position of the file pointer. In this file, the pointer moves from the current file pointer position.
2This value represents the reference position at is the end and moves the file pointer from the end of the file. In this case, the offset value must be negative

In this tutorial we use below give file for example mydataseek.txt

Welcome to mcaprogramming. Learn programming in easy ways

How to use seek method

try:
    file=open("mydataseek.txt");
    data=file.read(5);
    print("Current file pointer positionn",file.tell())
    print(data);
    #set the file pointer to the 10 position from the starting of the file
    file.seek(11)
    data=file.read();
    print(data,end="");   
    print("Current file pointer position",file.tell())    
finally:
    file.close()
Current file pointer position 5
Welco
mcaprogramming. Learn programming in easy ways
Current file pointer position 59

Form the python version 3 only support 0 as refrence value. For example file.seek(-10,2) is now supported for UTF-8 file encoding.

For more information refer Python Doc

Leave a Comment

Your email address will not be published. Required fields are marked *

Close Bitnami banner
Bitnami