Python3 File tell() 方法

Python3 File(文件) 方法 Python3 File(文件) 方法


概述

tell() 方法返回文件的當(dāng)前位置,即文件指針當(dāng)前位置。

語(yǔ)法

tell() 方法語(yǔ)法如下:

fileObject.tell(offset[, whence])

參數(shù)

  • 無(wú)

返回值

返回文件的當(dāng)前位置。

實(shí)例

以下實(shí)例演示了 tell() 方法的使用:

文件 youj.txt 的內(nèi)容如下:

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

循環(huán)讀取文件的內(nèi)容:

#!/usr/bin/python3

# 打開(kāi)文件
fo = open("youj.txt", "r+")
print ("文件名為: ", fo.name)

line = fo.readline()
print ("讀取的數(shù)據(jù)為: %s" % (line))

# 獲取當(dāng)前文件位置
pos = fo.tell()
print ("當(dāng)前位置: %d" % (pos))


# 關(guān)閉文件
fo.close()

以上實(shí)例輸出結(jié)果為:

文件名為:  youj.txt
讀取的數(shù)據(jù)為: 1:www.o2fo.com

當(dāng)前位置: 17

Python3 File(文件) 方法 Python3 File(文件) 方法