您好,欢迎访问一九零五行业门户网

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

tell()方法返回的文件内的文件读/写指针的当前位置。
语法
以下是tell()方法的语法:
fileobject.tell()
参数
na返回值
此方法返回该文件中读出的文件/写指针的当前位置。
例子
下面的例子显示了tell()方法的使用。
#!/usr/bin/python# open a filefo = 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 lineline = fo.readline()print read line: %s % (line)# get the current position of the file.pos = fo.tell()print current position: %d % (pos)# close opend filefo.close()
当我们运行上面的程序,它会产生以下结果:
name of the file: foo.txtread line: this is 1st linecurrent position: 18
其它类似信息

推荐信息