Python中tell()方法的使用详解
tell()方法返回的文件内的文件读/写指针的当前位置。
语法
以下是tell()方法的语法:
fileObject.tell()
参数
- NA
返回值
此方法返回该文件中读出的文件/写指针的当前位置。
例子
下面的例子显示了tell()方法的使用。
#!/usr/bin/python
#Openafile
fo=open("foo.txt","rw+")
print"Nameofthefile:",fo.name
#Assumingfilehasfollowing5lines
#Thisis1stline
#Thisis2ndline
#Thisis3rdline
#Thisis4thline
#Thisis5thline
line=fo.readline()
print"ReadLine:%s"%(line)
#Getthecurrentpositionofthefile.
pos=fo.tell()
print"CurrentPosition:%d"%(pos)
#Closeopendfile
fo.close()
当我们运行上面的程序,它会产生以下结果:
Nameofthefile:foo.txt ReadLine:Thisis1stline CurrentPosition:18