Python tell() function

Python too supports file handling and provides inbuilt functions for creating, writing and reading files. There are two types of files that can be handled in python, normal text files and binary files (written in binary language,0s and 1s).

  • Text files: In this type of file, Each line of text is terminated with a special character called EOL (End of Line), which is the new line character (‘\n’) in python by default.
  • Binary files: In this type of file, there is no terminator for a line and the data is stored after converting it into machine understandable binary language.

Refer the below articles to get the idea about basics of File handling.

tell() method:

Access modes govern the type of operations possible in the opened file. It refers to how the file will be used once it’s opened. These modes also define the location of the File Handle in the file. File handle is like a cursor, which defines from where the data has to be read or written in the file. Sometimes it becomes important for us to know the position of the File Hanlde. tell() method can be used to get the position of File Handle. tell() method returns current position of file object. This method takes no parameters and returns an integer value. Initially file pointer points to the beginning of the file(if not opened in append mode). So, the initial value of tell() is zero.



syntax :

file_object.tell()

Let’s suppose the text file named “myfile” looks like this:

python-tell()

# Example 1: Position of File Handle before reading or writing to file.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to demonstrate
# tell() method
  
  
# Open the file in read mode
fp = open("myfile.txt", "r")
  
# Print the position of handle
print(fp.tell())
  
#Closing file
fp.close()

chevron_right


output :

0

# Example 2: Position of File Handle after reading data from file.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to demonstrate
# tell() method
  
# Opening file
fp = open("sample.txt", "r")
fp.read(8)
  
# Print the position of handle
print(fp.tell())
  
# Closing file
fp.close()

chevron_right


Output :

8

# Example 3: For binary files. Let’s create a binary file and we will notice the position of handle before writing and after writing to binary file.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to demonstrate
# tell() method
  
# for reading binary file we
# have to use "wb" in file mode.
fp = open("sample2.txt", "wb")
print(fp.tell())
  
# Writing to file
fp.write(b'1010101')
  
print(fp.tell())
  
# Closing file
fp.close()

chevron_right


Output :

0
7



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.