Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Python tell function
2.1.
Syntax:
3.
Examples
4.
Frequently Asked Questions
4.1.
What is Python tell function?
4.2.
How many arguments are passed to the tell() method?
4.3.
What is seek() function?
4.4.
How many parameters does the seek () method takes?
4.5.
What does readline() do in Python?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Python tell() function

Introduction

Before coming to the topic, let's briefly discuss Python.

Python is a free, open-source programming language with a simple syntax often used to build websites and software, automate tasks, and conduct data analysis.

Python has become a popular programming language as it runs on an interpreter system and is easy to learn and implement.

Python supports file handling and provides inbuilt functions for creating, writing, and reading files. There are generally two types of files that can be handled in Python, i.e., Text Files and Binary Files.

Python tell() function Image

Now coming to the topic, i.e., Python tell() function. Let's discuss Python tell function with their syntax and some examples related to the Python tell function.

Also read about, Floor Division in Python, and Convert String to List Python

Python tell function

Python tell function is used to determine the current position or location of the file pointer.

Why Python tell function is important.

As the Pyhton tell function returns the current position of the file read/write pointer within the file, and sometimes it becomes essential for us to know the position.

Python tell function doesn't take any parameters and returns an integer value, i.e., the current position of the file stream.

Also, the initial value of the Pyhton tell function is 0.

Now, let's see the syntax of the Python tell function.

Syntax:

file.tell()
You can also try this code with Online Python Compiler
Run Code

 

Let's discuss some examples of the Python tell function.

Examples

Example Image

Example1: In this example, we find the position of the file handle before reading the file.

fp = open("Example1.txt", "r")
# Printing the position of the handle
print(fp.tell())
#Closing file
fp.close()
You can also try this code with Online Python Compiler
Run Code


Output:

0


Example2: In this example, we find the position of the file handle before reading the file.

fp = open("Example1.txt", "r")
fp.read(5)
# Print the position of the handle
print(fp.tell())
# Closing file
fp.close()
You can also try this code with Online Python Compiler
Run Code


Output:

5


Example3: In this example, we created a binary file and will find the position before writing and after writing to the binary file. 

Example 3 Image (Binary File)

fp = open("Example1.txt", "wb")
print(fp.tell())
# Writing to file
fp.write(b'10101')
print(fp.tell())
# Closing file
fp.close()
You can also try this code with Online Python Compiler
Run Code


Output:

0
5

 

You can practice by yourself with the help of online python compiler.
Let's see some frequently asked questions related to the Python tell function.

Also check out Python Square Root here.

Frequently Asked Questions

What is Python tell function?

It is a method used to find the current position of the file pointer.

How many arguments are passed to the tell() method?

This method takes no parameters and returns an integer value.

What is seek() function?

It is a function that moves the file pointer to the specified file position.

How many parameters does the seek () method takes?

This method takes two arguments.

What does readline() do in Python?

The readline() method returns one line from the file. However, by using the size parameter, You can also specify how many bytes from the line to return. 

Conclusion

We have discussed the Python tell function with its syntax and examples.

After reading about the Pyhton tell function, are you not feeling excited to read/explore more articles on Data Structures and Algorithms? Don't worry; Coding Ninjas has you covered. See PythonBasics of PythonFunctions in Python, Isupper in python, and Testing Framework in Python to learn.

Check out this article - Quicksort Python

 Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! 

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Live masterclass