Python3 File tell() 方法

Python3 File(檔) 方法 Python3 File(檔) 方法


概述

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

語法

tell() 方法語法如下:

fileObject.tell()

參數

返回值

返回檔的當前位置。

實例

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

檔 zaixian.txt 的內容如下:

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

迴圈讀取檔的內容:

實例(Python 3.0+)

#!/usr/bin/python3 # 打開檔 fo = open("zaixian.txt", "r+") print ("檔案名為: ", fo.name) line = fo.readline() print ("讀取的數據為: %s" % (line)) # 獲取當前檔位置 pos = fo.tell() print ("當前位置: %d" % (pos)) # 關閉檔 fo.close()

以上實例輸出結果為:

檔案名為:  zaixian.txt
讀取的數據為: 1:www.xuhuhu.com

當前位置: 17

Python3 File(檔) 方法 Python3 File(檔) 方法