Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
read
write
communitycreator

What is tell() in Python?

Mahnoor Zaheer

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The tell() function returns the position of the ‘read’ or ‘write’ pointer in a file.

Parameters

No parameters go inside the tell() function.

Syntax and return value

This function is used with the file object and is declared as:

The return value is an integer that indicates the current position of the pointer in the file in bits.

Code

Here, the tell() function is called upon the file object, which opens the text_file.txt file in read mode. This file is twenty bits long. Firstly, the read() method reads the first five bits of the file and prints them. Then, it reads it beyond the last bit, i.e, the twentieth bit. Since the file is twenty bits long, the tell() function cannot read the content beyond it and returns the total number of bits in the file.

main.py
text_file.txt
# opening the file
file_object = open("text_file.txt", "r")
# determining the position of the pointer
print(file_object.tell())
# read the first five character in the file and print them using the tell() function
print(file_object.read(5))
print(file_object.tell())
# read the remaining characters in the file and print them using the tell() function
print(file_object.read(21))
print(file_object.tell())
# closing the file
file_object.close()

The read/write position of binary files can also be determined through the tell() function. In the example below, binary_file.txt is an empty file. It is opened in "wb"also known as “write in binary” mode, and the tell() function shows the position of the pointer, which is zero. This indicates an empty file. Then, eleven bits are written into the file with the write() function, after which the tell() function determines the updated position of the pointer in the file.

main.py
binary_file.txt
# opening the binary file
fp = open("binary_file.txt", "wb")
# determining the position of the pointer
print(fp.tell())
# writing to the file
fp.write(b'10101010101')
# determining the position of the pointer
print(fp.tell())
# closing the file
fp.close()

RELATED TAGS

python
read
write
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring