×

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 memoryview() Function

Python memoryview() Function The memoryview() function in Python returns a memory view object from a specified object. Syntax memoryview(obj) Parameter obj: This parameter represents a Bytes object or a bytearray object. Return This function returns a “memory view” object...

1 minute read.

Amazon rekognition using python

Amazon recognition service is an AI service which is an image labelling API (Application programming interface). In this article, we shall learn to use the AWS python boto3 module to...

3 minutes read.

Python String encode() method

Python String encode() method The string.encode() method in Python returns an encoded version of the string. The default encoding is the current default string encoding Syntax String.encode([encoding[,errors]]) Parameter Encoding: This parameter represents a String specifying...

2 minutes read.

Python RegEx

Python RegEx (python regular expressions) is a concept of writing and finding expressions in a pool of characters easily. A Regular expression (RegEx) is a set of characters that defines search...

10 minutes read.

Python String istittle() method

Python String istittle() method The string.istittle() method returns a boolean value true if the string is a titlecased string and there is at least one character, for example uppercase characters may...

2 minutes read.

Best app for python

What is python? Python is a prevalent, general-purpose, and high-level programming language. Python Programming Language is appropriate for experienced programmers and also for beginners. Python programming language is being utilized by...

19 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.

Python Line Break

Introduction In this tutorial, you will learn line breaks in python.In Python, the new line character is used to indicate the start of a new line and the end of an...

5 minutes read.

Polymorphism in Python

What is polymorphism and why is it important? Polymorphism's literal definition is the state of occurring in diverse shapes or forms. When it comes to programming, the idea of polymorphism is crucial....

3 minutes read.

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.

How to Print Pattern in Python

How to Print Pattern in Python. Pattern programs are really important for beginners and they are always asked in all the technical interviews. Multiple loops approach is used to print the different...

6 minutes read.

Python globals() function

Python globals() function The globals() function in Python returns the value of the named attribute of object where the ‘name’ must be a string. Syntax globals() Parameter NA Return This function returns the global symbol table as a dictionary. Example...

1 minute read.

Python BytesIO

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

3 minutes read.

Python Call Function

In this article, you will learn about calling a function in Python. But before learning this, we should know about functions in Python.So, let’s get some overview of functions in...

6 minutes read.

What is online python free IDE

An IDE is an acronym that stands for “Integrated development environment.” It is a software environment that has a software development package that consists of the code editor and builds...

4 minutes read.

Python Comment Block

In this tutorial, we will see what comment blocks mean in Python. Further, we will see the commenting methods supported in Python. We will understand the topics deeply with the...

6 minutes read.

chr() and ord() Functions in Python

Python language contains many built-in functions which we use for many programs to work efficiently. The chr() and ord() are a few built-in functions in Python that are used for...

4 minutes read.

Python whois

What is whois? Whois is a protocol used to identify the owner of the registered domain name. It is a querying database that is used to record the registered users. WHOIS is...

4 minutes read.

Django vs NodeJS

Django and NodeJS are open-source frameworks used to develop web applications. Both Django and NodeJS are cross-platform and robust technology used for developing versatile web applications and mobile applications. Django Django is...

2 minutes read.

Periodogram in Python

Python:  Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

3 minutes read.