Python 檔案 tell 方法

2022-07-05 00:33:11 字數 740 閱讀 3226

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

tell() 方法語法如下:

fileobject.tell()

返回檔案的當前位置。

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

檔案 runoob.txt 的內容如下:

1:www.runoob.com

2:www.runoob.com

3:www.runoob.com

4:www.runoob.com

5:www.runoob.com

迴圈讀取檔案的內容:

#!/usr/bin/python3

# 開啟檔案

fo = open("runoob.txt", "r+",encoding="utf-8")

print ("檔名為: ", fo.name)

line = fo.readline()

print ("讀取的資料為: %s" % (line))

# 獲取當前檔案位置

pos = fo.tell()

print ("當前位置: %d" % (pos))

# 關閉檔案

fo.close()

以上例項輸出結果為:

檔名為:  runoob.txt

讀取的資料為: 1:www.runoob.com

當前位置: 18

Python 檔案 tell 方法

python 檔案 tell 方法返回檔案的當前位置,即檔案指標當前位置。tell 方法語法如下 fileobject.tell 返回檔案的當前位置。以下例項演示了 tell 方法的使用 檔案 runoob.txt 的內容如下 1 www.runoob.com 2 www.runoob.com 3 ...

python檔案處理方法 python檔案處理

開啟檔案,當前目錄 help file 方法.close 關閉檔案。關閉後檔案不能再進行讀寫操作。flush 重新整理檔案內部緩衝,直接把內部緩衝區的資料立刻寫入檔案,而不是被動的等待輸出緩衝區寫入。fileno 返回乙個整型的檔案描述符 file descriptor fd 整型 可以用在如os模...

python 檔案內建方法

file.close 關閉檔案 file.fileno 返回檔案的描述符 file descriptor fd,整數值 file.flush 重新整理檔案的內部緩衝區 file.isatty 判斷 file 是否是乙個類 tty 裝置 a file.next 返回檔案的下一行 類似於 file.re...