Python File tell() Method

Python File(文件) 方法Python File Methods


Overview

tell()The method returns the current position of the file, that is, 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.

Example

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 to read the content of the file:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

# 打开文件
fo = open("example.txt", "rw+")
print "文件名为: ", fo.name

line = fo.readline()
print "读取的数据为: %s" % (line)

# 获取当前文件位置
pos = fo.tell()
print "当前位置: %d" % (pos)


# 关闭文件
fo.close()

The output of the above example is:

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

当前位置: 17

Python File(文件) 方法Python File Methods

Other Extensions