Python File tell() method
Overview
tell() The method returns the current position in the file, i.e. the current position of the file pointer.
Syntax
The tell() method syntax is as follows:
fileObject.tell()
Parameters
-
None
Return Value
Returns the current position in the file.
Example
The following example shows how to use the tell() method:
The contents of the runoob.txt file are as follows:
1:www.runoob.com
2:www.runoob.com
3:www.runoob.com
4:www.runoob.com
5:www.runoob.com
Read the file's contents in a loop:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# Open file
fo = open("runoob.txt", "rw+")
print "文件名为: ", fo.name
line = fo.readline()
print "读取的数据为: %s" % (line)
# Get current file path
pos = fo.tell()
print "当前位置: %d" % (pos)
# Close file
fo.close()
The output of the above example is:
文件名为: runoob.txt
读取的数据为: 1:www.runoob.com
当前位置: 17
Python File methods