Latest web development tutorials

Python File tell() 方法

Python File(文件) 方法 Python File(文件)方法


概述

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

語法

tell() 方法語法如下:

fileObject.tell(offset[, whence])

參數

返回值

返回文件的當前位置。

實例

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

文件w3big.txt 的內容如下:

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

循環讀取文件的內容:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

# 打开文件
fo = open("w3big.txt", "rw+")
print "文件名为: ", fo.name

line = fo.readline()
print "读取的数据为: %s" % (line)

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


# 关闭文件
fo.close()

以上實例輸出結果為:

文件名为:  w3big.txt
读取的数据为: 1:www.w3big.com

当前位置: 17

Python File(文件) 方法 Python File(文件)方法