| by Arround The Web | No comments

Python File Tell() Method

Python presents different methods for working and handling files. These methods are utilized to perform various operations/tasks such as reading, removing, renaming, or getting files information and others. Sometimes we need to retrieve the file handler or pointer position for reading or writing purposes. To achieve this, the “file.tell()” method is used in Python.

This write-up will propose a comprehensive tutorial via the below contents on the “file.tell()” method:

What is the Python file “tell()” Method?

The “tell()” method in Python is used to retrieve the current/present position of the file handle from the beginning. To change/modify the file handle position, the seek() method is used in Python.

Syntax

file_object.tell()

 

Parameters

None

Example 1: Determining the Position of the File Handle Utilizing the “tell()” Method

This example opens the file “example.txt” in “read” mode and uses the “tell()” method to retrieve the position handle of the file. After retrieving the position handle, the file is closed using the “file.close()” method:

file1 = open("example.txt", "r")
print(file1.tell())
file1.close()

 

The above code executes the following output:

Example 2: Determining the Position of the File Handle After Reading the File Data

The below code opens the file and reads specific data from the file using the “read()” method. After reading the data, the “tell()” method is used to tell the position of the handle after reading specific data.

file1 = open("example.txt", "r")
file1.read(8)
print(file1.tell())
file1.close()

 

The position of the file handle has been retrieved successfully:

Example 3: Determining the Position of the File Handle After Reading the Complete Line

We can also utilize the “tell()” method along with the “readline()” method to read the complete line and tell the position of the file handle. Here is an example code for getting the position of the file handle:

file1 = open("example.txt", "r")
file1.readline()
print(file1.tell())
file1.close()

 

The below snippet displays the position of the file handle:

Example 4: Determining the Position of the File Handle Before and After Writing to the File

The “tell()” method can be used to determine the position of the file handle before and after writing the file. The “file.write()” method is used to write the particular file string. Take the following code to understand this method:

file1 = open("example1.txt", "w")
print('Position of Handle Before Writing File:', file1.tell())
file1.write('Hello and Welcome to Python Guide!')
print('\nPosition of Handle After Writing File:', file1.tell())
file1.close()

 

The output of the above-described code is provided below:

Example 5: Determining the Position of the File Handle Before and After Writing to the Binary File

We can determine the position of the file handle of the binary file using the “tell()” method. The “open()” method creates the file in “wb” mode and writes the binary data to the file utilizing the “write()” method. The “tell()” method retrieves the position of the file handle of the binary file pre and post-file writing. Let’s use the following code to perform this method:

file1 = open("example2.txt", "wb")
print('Position of Handle Before Writing File:', file1.tell())
file1.write(b'1010101')
print('\nPosition of Handle After Writing File:', file1.tell())
file1.close()

 

The position of the file handle has been retrieved successfully:

Conclusion

In Python, the “file.tell()” method is utilized to retrieve the present/current position of the file pointer or handle from the beginning of the file. The “tell()” method can be used to retrieve the file handle position after reading specific data of the file using the “read()” method or writing to the file using the “write()” method. This blog delivered a detailed guide on the “file.tell()” method in Python using several examples.

Share Button

Source: linuxhint.com

Leave a Reply