Python File tell() 方法
概述
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
迴圈讀取檔的內容:
#!/usr/bin/python # -*- coding: UTF-8 -*- # 打開檔 fo = open("zaixian.txt", "rw+") print "檔案名為: ", fo.name line = fo.readline() print "讀取的數據為: %s" % (line) # 獲取當前檔位置 pos = fo.tell() print "當前位置: %d" % (pos) # 關閉檔 fo.close()
以上實例輸出結果為:
檔案名為: zaixian.txt 讀取的數據為: 1:www.xuhuhu.com 當前位置: 17