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 拜年红包的祝福语简短
18 妈妈生日祝福语简短励志