Python3 File tell() 方法

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


概述

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

語法

tell() 方法語法如下:

fileObject.tell(offset[, whence])

參數

返回值

返回文件的當前位置。

實例

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

文件 youj.txt 的內容如下:

1:www.w3cschool.cn
2:www.w3cschool.cn
3:www.w3cschool.cn
4:www.w3cschool.cn
5:www.w3cschool.cn

循環讀取文件的內容:

#!/usr/bin/python3

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

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

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


# 關閉文件
fo.close()

以上實例輸出結果為:

文件名為:  youj.txt
讀取的數據為: 1:www.w3cschool.cn

當前位置: 17

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