Python File tell() Method with Examples

Files In Python:

A file is a piece of data or information stored on a computer’s hard drive. You’re already familiar with a variety of file kinds, including music, video, and text files. Manipulation of these files is trivial with Python. Text files and binary files are the two types of files that are commonly used. Binary files contain binary data that can only be read by a computer, whereas text files include plain text.

For programmers and automation testers, Python file handling (also known as File I/O) is a crucial topic. Working with files is required in order to write to or read data from them.

In addition, if you didn’t know, I/O activities are the most expensive techniques via which software might fail. As a result, when implementing file processing for reporting or any other reason, you should proceed with caution. The construction of a high-performance application or a robust solution for automated software testing can benefit from optimizing a single file activity.

File tell() Method in Python:

The tell() is a built-in method that returns the position of the current file in a file stream.

Syntax:

file.tell()

Parameters: This method doesn’t accept any parameters.

Return Value:

This method’s return type is class <‘bool’>; it returns an integer value representing the file location in the file stream.

File tell() Method with Examples in Python

Example

Approach:

  • Make a single variable to store the path of the file. This is a constant value. This value must be replaced with the file path from your own system in the example below.
  • Open the file in write mode. In this case, we’re writing the contents into the file.
  • Apply tell() method to the given file to get the current file position and print it
  • Write some random content to the file using the write() function.
  • Apply tell() method to the given file to get the current file position and print it.
  • Again, Write some random content to the file using the write() function.
  • Again, Apply tell() method to the given file to get the current file position and print it.
  • Close the given file using the close() function
  • The Exit of Program.

Below is the implementation:

# Make a single variable to store the path of the file. This is a constant value.
# This value must be replaced with the file path from your own system in the example below.
givenFilename = "samplefile.txt"
# Open the file in write mode. In this case, we're writing the contents into the file.
gvn_file = open(givenFilename, 'w') 
# Apply tell() method to the given file to get the current file position and print it
print("After Applying tell() method to the given file:")
print(gvn_file.tell())
# Write some random content to the file using the write() function.
gvn_file.write("Hello this is btechgeeks")
# Apply tell() method to the given file to get the current file position and print it
print("After writing content and Applying tell() method to the given file:")
print(gvn_file.tell())
# Again, Write some random content to the file using the write() function.
gvn_file.write("Good morning btechgeeks")
# Again, Apply tell() method to the given file to get the current file position and print it
print("After writing more content and Applying tell() method to the given file:")
print(gvn_file.tell())
# Close the given file using the close() function
gvn_file.close()

Output:

After Applying tell() method to the given file:
0 
After writing content and Applying tell() method to the given file: 
24 
After writing more content and Applying tell() method to the given file: 
47

File Content:

Hello this is btechgeeksGood morning btechgeeks

Google Colab Images:

Files and Code: