Python File tell() Method



The Python File tell() method is used to find the current position of the file cursor (or pointer) within the file.

This method is mostly used in scenarios where there is a need to determine whether the file cursor is either at the beginning of the file or the ending.

Syntax

Following is the syntax for tell() method −

fileObject.tell()

Parameters

The method does not accept any parameters.

Return Value

This method returns the current position of the file read/write pointer within the file.

Example

Consider a demo file "foo.txt" containing 5 lines. Let us try to call the Python File tell() method on this file, in various scenarios.

This is 1st line
This is 2nd line
This is 3rd line
This is 4th line
This is 5th line

The following example shows the usage of the Python File tell() method. In here, we will use the readline() method trying to read the first line in the demo file. Then, the tell() method is called to determine the current position of the file pointer.

# Open a file
fo = open("foo.txt", "r+")
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 line

line = fo.readline()
print("Read Line:", line)

# Get the current position of the file.
pos = fo.tell()
print("Current Position:", pos)

# Close opened file
fo.close()

When we run above program, it produces following result −

Name of the file:  foo.txt
Read Line: This is 1st line

Current Position: 18

Example

Using the tell() method, we can also write into a file at a certain position. In the following example, we are writing into an empty file and determine the final file pointer position using this method.

# Open a file
fo = open("demo.txt", "w")
print("Name of the file: ", fo.name)

# Write into the file using write() method
fo.write("This is a demo file")

# Get the current position of the file.
pos = fo.tell()
print("Current Position:", pos)

# Close opened file
fo.close()

Once we execute the program above, the result is produced as follows −

Name of the file:  demo.txt
Current Position: 19

Example

In this example, we will try to determine the cursor position every time a line is appended into a demo file. Firstly, we open a file in the append mode (a or a+) and display the file cursor position using the tell() method. Then, we append new content into the file using the write() method. The final position of the cursor is noted again.

# Open a file
fo = open("demo.txt", "a")
print("Name of the file: ", fo.name)

# Get the current position of the file.
pos = fo.tell()
print("Current Position:", pos)

# Write into the file using write() method
fo.write("Tutorialspoint")

# Get the current position of the file after appending.
pos = fo.tell()
print("Position after appending:", pos)

# Close opened file
fo.close()

The output for the program above is produced as follows −

Name of the file:  demo.txt
Current Position: 19
Position after appending: 33

Example

The tell() method goes hand-in-hand with the seek() method. In the following example, we are trying to use the seek() method to set the file cursor at a specific position and then, use the tell() method to retrieve this position set.

# Open a file
fo = open("foo.txt", "r")
print("Name of the file: ", fo.name)

# Move the pointer backwards using negative offset
fo.seek(18, 0)

line = fo.read()
print("File Contents:", line)

#Using tell() method retrieve the cursor position from the ending
print("File cursor is present at position", fo.tell())

# Close opened file
fo.close()

On executing the program above, the output is displayed as −

Name of the file:  foo.txt
File Contents: This is 2nd line
This is 3rd line
This is 4th line
This is 5th line
File cursor is present at position 88
python_file_methods.htm
Advertisements