Python 文件 tell()方法
tell()方法返回文件中的当前读/写指针的位置。
语法
tell()方法的语法如下 –
fileObject.tell()
参数
NA
返回值
该方法返回文件中读/写指针的当前位置。
下面的示例演示了tell()方法的用法 –
假设’foo.txt’文件包含以下文本 –
This is 1st line
This is 2nd line
This is 3rd line
This is 4th line
This is 5th line
示例
fo = open("foo.txt", "r+")
print ("Name of the file: ", fo.name)
line = fo.readline()
print ("Read Line: %s" % (line))
pos=fo.tell()
print ("current position : ",pos)
# Close the opened file
fo.close()
当我们运行上述程序时,它会产生以下输出。
Name of the file: foo.txt
Read Line: This is 1st line
Current Position: 18