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
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短