The use of the tell () method in Python
The current position of the file read/write pointer in the file returned by the tell () method.
Syntax
The syntax of the tell () method is as follows:
FileObject. tell ()
Parameters
Return Value
This method returns the current position of the file read/write pointer in the file.
Example
The following example shows how to use the tell () method.
#! /Usr/bin/python # Open a filefo = open ("foo.txt", "rw +") print "Name of the file:", fo. name # Assuming file has following 5 lines # This is 1st line # This is 2nd line # This is 3rd line # This is 4th line # This is 5th lineline = fo. readline () print "Read Line: % s" % (line) # Get the current position of the file. pos = fo. tell () print "Current Position: % d" % (pos) # Close opend filefo. close ()
When we run the above program, it will produce the following results:
Name of the file: foo.txt Read Line: This is 1st lineCurrent Position: 18