×

tell() function in Python

Introduction

The tell() function in Python is used to return the current position of the file read/write pointer within the file. An integer value is returned by this method, and it doesn't take any parameter.

The access modes define the file handle position within the file. File handle can be defined as the cursor that defines the position of data to be read/written in the file.

The tell() method is helpful for knowing the position of the File handle.

The current position is retrieved with this method. The initial value of tell() is zero because at initial it points to the starting of the file.

syntax :

file_object.tell()

Considering that 'file.txt' file includes the following text:

tell() Function In Python

Example: Printing the current position.

# Example for demonstrating

# tell() method

# Opening the file

fp = open("C:/Users/Kaushal rajput/Desktop/file.txt", "r")

print ("File name: ", fp.name)

line = fp.readline()

print ("Read Line: %s" % (line))

# priting the current position

pos=fp.tell()

print ("current position: ",pos)

# Close opened file

fo.close()

Output:

Name of the file: C:/Users/Kaushal rajput/Desktop/file.txt

Read Line: Welcome to Tutorial and Example

current position: 33

Example: File Handle position before read/writes to file.

# Example for demonstrating

# tell() method

# Opening the file in read mode

fp = open("C:/Users/Kaushal rajput/Desktop/file.txt", "r")

# Print the position of file handle

print(fp.tell())

#Closing the file

fp.close()

Output :

0

Example: File Handle position after reading data from the file.

# Example for demonstrating

# tell() method

# Opening the file in read mode

fp = open("C:/Users/Kaushal rajput/Desktop/file.txt", "r")

fp.read(8)

# Print the position of file handle

print(fp.tell())

#Closing the file

fp.close()

Output:

'Welcome t'

9

Example: Get the position of a binary file before and after writing to the binary file.

# Example for demonstrating

# tell() method

# Using "wb" in the file mode for reading the binary file

fp = open("bin.txt", "wb")

print(fp.tell())

# Writing to file

fp.write(b'010101011')

print(fp.tell())

# Closing file

fp.close()

Output:

0

9

Conclusion

In the above article, we have learned about the tell() function in Python. We have seen how to use the tell() function for getting the current position of the file read/write pointer within the file.


Related Topics

Python And Operator

Python's and operator takes two operands that can be either object, Boolean expressions, or both. And operator creates more complex expressions using those operands. Conditions are the common name for...

8 minutes read.

Raise Exception in Python

Most of the programmers/developers create large programs to solve complex tasks in Python. The code can be of 1000 lines and even more based on the need of the problem....

5 minutes read.

Adding item to a python dictionary

The dictionary is one of python’s built-in data structures where it stores key-value pairs. A dictionary is a collection of ordered values that can be changeable. We can add, modify,...

3 minutes read.

Accuracy_score Function in Sklearn

A crucial stage in data science is measuring our model's performance using the appropriate metric. In this article, we will examine two methods for calculating the accuracy of your predictions:...

13 minutes read.

Python Generator

Python Generator: A Function is said to be a Python Generator that produces or generates a sequence of results. A Python Generator maintains its native state to work so that...

6 minutes read.

Python Dictionary get() method

Python Dictionary get() method The dictionary.get() method returns the value of the item with the specified key. Syntax dictionary.get(keyname, value) Parameter keyname- This parameter represents the key to be searched in the dictionary. value- The parameter...

1 minute read.

List Comprehension in Python

Sometimes we need new lists that are completely based on previously existing lists, so to perform this operation successfully, some of the programming languages come with a syntactic construct known...

5 minutes read.

ComboBox in Python

Python includes several graphical user interface (GUI) libraries, including PyQT, Tkinter, Kivy, WxPython, and PySide. Tkinter is the most commonly used GUI module in Python because it is simple and...

2 minutes read.

Python MySQL Client

MySQL client is a project that is the subdivision of the MySQLdb package. This package provides an interface that runs the Python database API. Installing mysqlclient You can easily install mysqlclient with...

5 minutes read.

Python Permutations and Combinations

In mathematics, we all studied what is meant by permutations and combinations. “Permutations” define the way of arranging the elements in sequential order. “Combinations” mean the way of selecting the...

7 minutes read.

How to Convert A List into String In Python?

How to Convert A List into String In Python? List is a data structure in Python that can hold values of different data types. The values are enclosed in square brackets...

3 minutes read.

Python if statement

Python if statement Decision making is an essential feature of any programming languages. Condition checking is the strength of decision making. Python provides many decision-making statements, such as: If statementIf-else statementelif statement if statement The if statement is...

2 minutes read.

Artificial intelligence mini projects with source code in Python

Project Name: Movie recommendation system A recommendation provides customers with relevant information related to their searches. Before the recommendation system, the most common method of purchasing was to rely on the...

4 minutes read.

Python Tuple Methods

Python Tuple Methods Python has two built-in methods that are used for tuples. The following are the two methods: Method Description count() The tuple.count() method in Python returns the number of times a...

1 minute read.

Python program to convert Celsius into Fahrenheit

Python program to convert Celsius into Fahrenheit This program explains how we can take the temperature in Celsius and convert them into Fahrenheit. Celsius Celsius, also known as centigrade, is a measurement unit...

1 minute read.

Python dir() function

Python dir() function The dir() function in Python returns all properties and methods of the specified object, without the values. Syntax dir([object]) Parameter object: This parameter represents the object one wants to see the valid attributes. Return This function...

2 minutes read.

Python Operators

Operators in Python programming An operator is a symbol that operates on one or more operands. An operand is a value or a variable on which we perform the operation....

5 minutes read.

Python Tricks: The Book

A Buffet of Awesome Python Features Dan Bader is the author of the book called Python Tricks . he is the owner and editor of Real Python and one of the...

3 minutes read.

Python range() function

Python range() function The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number. Syntax range(stop)        or range(start, stop[, step]) Parameter start: It is...

1 minute read.

Difference between For Loop and While Loop in Python

What is the “For” loop? The “For” Loop is a commonly used control structure in programming that allows us to repeat a set of actions multiple times. The “For” loop is...

3 minutes read.