Latest web development tutorials

Python3 File tell () method

Python3 File (File) method Python3 File (File) method


Outline

tell () method returns the file's current position, that is the current position of the file pointer.

grammar

tell () method has the following syntax:

fileObject.tell(offset[, whence])

parameter

  • no

return value

Returns the current location of the file.

Examples

The following example demonstrates the use tell () method:

W3big.txt content file as follows:

1:www.w3big.com
2:www.w3big.com
3:www.w3big.com
4:www.w3big.com
5:www.w3big.com

Loop reads the contents of the file:

#!/usr/bin/python3

# 打开文件
fo = open("w3big.txt", "r+")
print ("文件名为: ", fo.name)

line = fo.readline()
print ("读取的数据为: %s" % (line))

# 获取当前文件位置
pos = fo.tell()
print ("当前位置: %d" % (pos))


# 关闭文件
fo.close()

The above example output is:

文件名为:  w3big.txt
读取的数据为: 1:www.w3big.com

当前位置: 17

Python3 File (File) method Python3 File (File) method