How to Use tell() Method in Python?

In a computer, a file is just an entity that stores information. Python has a number of techniques for reading and managing files, which are referred to as file handling operations. They allow you to create, open, read, write, and modify files on your file system.
Here, let us see one such file handling approach i.e, the tell() method.

When you open a file, you expect to conduct a variety of operations on it. When you wish to read/write data to/from a file, you need a reference pointer to tell you where to begin. The file pointer, also known as a file object or file handle, handles this.
In simplified terms, the file object indicates the current position in the file and is useful in indicating where the next read/write operations will begin. (To understand, simply relate it to the cursor.)

You may want to know about this position sometimes

tell() method:

The tell() method returns the current position of the file object pointer within the file.

Syntax:

fileobject.tell()

Parameters: It doesn’t accept any parameters

Return Value:

The position of the file object is returned by the tell() method

The tell() Method in Python

Let us take a file say “samplefile.txt”

“samplefile.txt” file:

hello this is btechgeeks

Example1

Approach:

  • Open the file in read-only mode using the open() function
  • Apply tell() function on the above file object to get the position of the file pointer.
  • The Exit of the Program.

Below is the implementation:

# Open the file in read-only mode using the open() function
fileobj =open("samplefile.txt","r")
# Apply tell() function on the above file object to get the position of the 
# file pointer
print(fileobj.tell())

Output:

0

Explanation:

When a file is opened, the file pointer is at the beginning of the file. Hence the tell() method returns 0.

Example2

# Open the file in read-only mode using the open() function
fileobj =open("samplefile.txt","r")
# Apply tell() function on the above file object to get the position of the 
# file pointer
print(fileobj.tell())
# Read 5 letters from the given file using the read() function
print(fileobj.read(5))
# Now, Check the position of the file pointer using the tell() method
print(fileobj.tell())

Output:

0
hello
5

Explanation:

  • Here when the file is opened the pointer is at position 0.
  • Then, after reading 5 letters from the file, the pointer moves to position 5.

Example3

# Open the file in append mode using the open() function
fileobj =open("samplefile.txt", "a")
# Apply tell() function on the above file object to get the position of the 
# file pointer
print(fileobj.tell())

Output:

24

Explanation:

The append mode allows you to append data from the end of an existing data set. 
As a result, the file pointer position is at the end.

Example4: Opening the file in write mode

Approach:

  • Open the file in write mode using the open() function
  • Apply tell() function on the above file object to get the position of the file pointer and print it
  • Here the file pointer is at the beginning of the file.
  • Add some random data to the file using the write() method
  • Again apply tell() function on the above file object to get the position of the file pointer and print it
  • Here the file pointer moves to the end position of the text added.
  • The Exit of the Program.

Below is the implementation:

# Open the file in write mode using the open() function
fileobj =open("demofile.txt", "w")
# Apply tell() function on the above file object to get the position of the 
# file pointer and print it
# Here the file pointer is at the begining of the file.
print("The file pointer position at the beginning of the file:")
print(fileobj.tell())
# Add some random data to the file using the write() method
fileobj.write("goodmorning")
# Again apply tell() function on the above file object to get the position of the 
# file pointer and print it
# Here the file pointer moves to the end position of the text added.
print("The file pointer position after adding some random text to the file:")
print(fileobj.tell())

Output:

The file pointer position at the beginning of the file:
0
The file pointer position after adding some random text to the file:
11