English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python Basic Tutorial

Python Flow Control

Fonction en Python

Types de données en Python

Python File Operation

Python Objects and Classes

Python Date and Time

Advanced Knowledge of Python

Python Reference Manual

Python file tell() usage and example

Python File (File) Methods

Overview

tell() The method returns the current position of the file, that is, the position of the file pointer.

Syntax

Syntax of tell() method:

fileObject.tell()

Parameter

  • None

Return value

Return the current position of the file.

Example

The following example demonstrates the use of the tell() method:

File w3The content of codebox.txt is as follows:

1:fr.oldtoolbag.com
2:fr.oldtoolbag.com
3:fr.oldtoolbag.com
4:fr.oldtoolbag.com
5:fr.oldtoolbag.com

Loop to read the content of the file:

# Open the file
fo = open("w3codebox.txt", "r")
print("The file name is: ", fo.name)
line = fo.readline()
print("The data read is: %s" % (line))
# Get the current file position
pos = fo.tell()
print("Current location: %d" % (pos))
# Close the file
fo.close()

The output result of the above example is:

The file name is:  w3codebox.txt
The data read is: 1:fr.oldtoolbag.com
Current location: 17

Python File (File) Methods