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

yipeiwu_com5年前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利用pandas将excel文件转换为txt文件的方法

python将数据换为txt的方法有很多,可以用xlrd库实现。本人比较懒,不想按太多用的少的插件,利用已有库pandas将excel文件转换为txt文件。 直接上代码: ''' f...

python简易实现任意位数的水仙花实例

如下所示: # -*- coding: utf-8 -*- # 水仙花数是指一个 n 位正整数 ( n≥3 ),它的每个位上的数字的 n 次幂之和等于它本身。 # 要求:打印输出所有...

python文件操作整理汇总

总是记不住API。昨晚写的时候用到了这些,但是没记住,于是就索性整理一下吧: python中对文件、文件夹(文件操作函数)的操作需要涉及到os模块和shutil模块。 得到当前工作目录,...

Python 实现12306登录功能实例代码

下面一段代码给大家带来了python实现12306登录功能,具体代码如下所示: #!/usr/bin/env python import requests import urllib...

OpenCV里的imshow()和Matplotlib.pyplot的imshow()的实现

OpenCV里的imshow()和Matplotlib.pyplot的imshow()的实现

一、问题 在Python里使用OpenCV时,一般是通过cv2.imread读入图片,然后用plt.imshow显示图片,但最近学习OpenCV时这样做的结果与预期的结果有较大的出入。查...