Python Tutorial Python Advanced Python References Python Libraries

Python - File tell() Method



The Python tell() method returns the current position in the file stream.

Syntax

file.tell()

Parameters

No parameter is required.

Return Value

Returns the current position in the file stream.

Example: Return current position in the file stream

Lets assume that we have a file called test.txt. This file contains following content:

This is a test file.
It contains dummy content.

In the example below, it is opened in read mode to read first two lines. After reading each line, tell() method is used to find out the current position in the file stream.

#read the file
MyFile = open("test.txt", "r")
#read first line of the file
MyFile.readline()
#Current position in the file stream
print("File stream position:", MyFile.tell())

#read the second line of the file
MyFile.readline()
#Current position in the file stream
print("File stream position:", MyFile.tell())

MyFile.close()

The output of the above code will be:

File stream position: 22
File stream position: 48

❮ Python File Handling Methods

5