目录

file.tell()

描述 (Description)

方法tell()返回文件中文件读/写指针的当前位置。

语法 (Syntax)

以下是tell()方法的语法 -

fileObject.tell()

参数 (Parameters)

  • NA

返回值 (Return Value)

此方法返回文件中文件读/写指针的当前位置。

例子 (Example)

以下示例显示了tell()方法的用法。

This is 1st line
This is 2nd line
This is 3rd line
This is 4th line
This is 5th line

#!/usr/bin/python
# Open a file
fo = open("foo.txt", "rw+")
print "Name of the file: ", fo.name
# Assuming file has following 5 lines
# This is 1st line
# This is 2nd line
# This is 3rd line
# This is 4th line
# This is 5th line
line = fo.readline()
print "Read Line: %s" % (line)
# Get the current position of the file.
pos = fo.tell()
print "Current Position: %d" % (pos)
# Close opend file
fo.close()

当我们运行上面的程序时,它产生以下结果 -

Name of the file:  foo.txt
Read Line: Python is a great language.
Current Position: 28
↑回到顶部↑
WIKI教程 @2018