Python中tell()方法的使用详解

yipeiwu_com6年前Python基础

 tell()方法返回的文件内的文件读/写指针的当前位置。
语法

以下是tell()方法的语法:

fileObject.tell()

参数

  •     NA

返回值

此方法返回该文件中读出的文件/写指针的当前位置。
例子

下面的例子显示了tell()方法的使用。

#!/usr/bin/python

# Open a file
fo = open("foo.txt", "rw+")
print "Name of the file: ", fo.name

# Assuming file has following 5 lines
# This is 1st line
# This is 2nd line
# This is 3rd line
# This is 4th line
# This is 5th line

line = fo.readline()
print "Read Line: %s" % (line)

# Get the current position of the file.
pos = fo.tell()
print "Current Position: %d" % (pos)

# Close opend file
fo.close()

当我们运行上面的程序,它会产生以下结果:

Name of the file: foo.txt
Read Line: This is 1st line

Current Position: 18


相关文章

python使用os模块的os.walk遍历文件夹示例

复制代码 代码如下:#-*- coding:utf-8 -*- import os if __name__ == '__main__':    try:&n...

python用pandas数据加载、存储与文件格式的实例

数据加载、存储与文件格式 pandas提供了一些用于将表格型数据读取为DataFrame对象的函数。其中read_csv和read_talbe用得最多 pandas中的解析函数: 函数...

常见python正则用法的简单实例

下面列出Python正则表达式的几种匹配用法: 1.测试正则表达式是否匹配字符串的全部或部分 regex=ur"" #正则表达式 if re.search(regex, subj...

Python函数中的函数(闭包)用法实例

本文实例讲述了Python闭包的用法。分享给大家供大家参考,具体如下: Python函数中也可以定义函数,也就是闭包。跟js中的闭包概念其实差不多,举个Python中闭包的例子。 d...

python实现银联支付和支付宝支付接入

本文实例为大家分享了python银联支付和支付宝支付接入的具体代码,供大家参考,具体内容如下 前置条件:需要安装Python的OpenSSL模块,我使用的版本是16.1.0,可以使用pi...