×

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

Dictionary to JSON Python

In python JSON (JavascriptObject Notation). In the programming language, the text file is made using the script file.We can use many built-in packages which arenamedJSON.Before using the packages, we have...

3 minutes read.

Python CGI Programming

The Concept of CGI CGI is an abbreviation for Common Gateway Interface. It is not a type of language but a set of rules (specification) that establishes a dynamic interaction between...

9 minutes read.

Python program to count the number of a substring in a string

Python program to count the number of a substring in a string A part of the string is called a substring. This article explains the Python program to find how many...

1 minute read.

Spell Corrector GUI using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

3 minutes read.

Length of Tuple in Python

What is Tuple? Python is a data structure in a python programming language; it is the collection of the objects in a sequence. The tuples are immutable; that is, we cannot...

3 minutes read.

Python Linked List

Linked List Linked lists non-contiguous linear data structure which is made up of nodes and used to store value and a pointer pointing to the next node. It is linked with...

6 minutes read.

Image to NumPy Arrays in Python

Image is a simple process of working model confined to machine learning, Python works with the image in the data format of width and height i.e. Images are excelled into...

3 minutes read.

Array to String in Python

What is an Array: The idea behind an array is to group several items of the same type, making it easier to locate each element by adding an extra offset to...

4 minutes read.

Python Rest API

In this tutorial, we will understand the meaning of API and REST API. We will understand the working of REST API. We will then realize the boundaries of architecture defined...

4 minutes read.

Exrex Python

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster...

4 minutes read.

Python Stack

Python Stack: The work Stack is defined as arranging a pile of objects or items on top of another. It is the same method of allocating memory in the stack...

10 minutes read.

Imread Python

In this walkthrough, we'll go over the specifics of using the imread() method of OpenCV-Python and various methods for loading images. Imread is an Image reading library. One of the most helpful...

7 minutes read.

Python List sort() method

The list.sort () method in Python sorts the items of the list in place. Syntax list.sort(key=None, reverse=False) Parameter reverse: If a Boolean value ‘True’ is passed, the  sorting will be done in the descending order else for ‘False’...

2 minutes read.

N2 in Python

N2 is known as Nearest Neighbor Algorithm, because it contains 2 N's (N-Nearest, N-Neighbor). This is a library in the python build using C++ and Python. Before N2 was made,...

3 minutes read.

Python Set intersection() method

Python Set intersection() method The set.intersection() method in Python returns a new set with elements that are similar between two or more sets. Syntax set.intersection(set1, set2 ... etc) Parameter set1- This parameter represents the set to search for...

2 minutes read.

Python Project Ideas Based On Django

Introduction If you have learned Python and you are an expert in Django, then your practical skills should be excellent in this field. If you want to check your practical skills,...

9 minutes read.

Python String zfill() method

Python String zfill() method The string.zfill() method in Python returns a copy of the string while adding the zeros (0) at the beginning of the string, until it reaches the specified...

1 minute read.

Python EOL (End Of Line)

Introduction An EOL (End Of Line) is defined as a syntax error that indicates that the Python interpreter reached at the end of the line when it tried to scan a...

3 minutes read.

Python program to find factorial of a given number

Python program to find factorial of a given number Factorial is a non-negative integer. It is the product of all positive integers from 1 to the number we are going to...

3 minutes read.

What is Python compiler GDB?

The source code of one programming language is converted into machine code, bytecode, or another programming language by a compiler, a specialised software. A compiler is a tool that converts high-level...

3 minutes read.