Python - File tell() Method
The Python file tell() method is used to return 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
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") #Assuming the file contains #This is line 1 content. #This is line 2 content. #This is line 3 content. #Current position in the file stream print("File stream position:", MyFile.tell()) #read first line from the file MyFile.readline() #After reading first line, #Current position in the file stream print("File stream position:", MyFile.tell()) #Close the open file MyFile.close()
The output of the above code will be:
File stream position: 0 File stream position: 25
❮ Python File Handling Methods