Python3 File tell() Method

Python3 File(文件) 方法Python3 File Methods


Overview

tell()The method returns the current position of the file, i.e., the current position of the file pointer.

Syntax

The syntax of the tell() method is as follows:

fileObject.tell()

Parameters

  • None

Return Value

Returns the current position of the file.

Examples

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

The content of the file example.txt is as follows:

1:www.example.com
2:www.example.com
3:www.example.com
4:www.example.com
5:www.example.com

Loop through and read the content of the file:

Examples (Python 3.0+)

#!/usr/bin/python3 # Open file fo = open("example.txt", "r+") print ("File name:", fo.name) line = fo.readline() print ("Data read: %s" % (line)) # Get current file position pos = fo.tell() print ("Current position: %d" % (pos)) # Close file fo.close()

The above example outputs:

文件名为:  example.txt
读取的数据为: 1:www.example.com

当前位置: 17

Python3 File(文件) 方法Python3 File Methods

Other Extensions