python中tell()方法的使用详解

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

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

fileobject.tell()

参数

na

返回值

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

下面的例子显示了tell()方法的使用。

#!/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: this is 1st line
current position: 18

Posted in 未分类

发表评论