Python - File tell() Method


Advertisements

The Python file tell() method is used to return the current position in the file stream.

Syntax


file.tell()

Parameters

No parameter is required.

Example: Return current position in the file stream

In the below example, a file called MyFile is opened in the read mode. Initially, the file stream position for this file is 0. After that, the first line is read using readline method which leads to change in the file stream position. Finally, the tell() method is again used to find out the current position in the file stream.

MyFile = open("python_test.txt", "r")

#Current position in the file stream
print("File stream position (at the start):", MyFile.tell())

#read first line from the file
MyFile.readline()

#Current position in the file stream
print("File stream position (after reading first line):", MyFile.tell())

#Close the open file
MyFile.close()

Output

File stream position (at the start): 0
File stream position (after reading first line): 35

Python File Handling Methods
Advertisements