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中使用enumerate函数遍历元素实例

这个是python的一个内建函数,看书的时候发现了他,mark一下当我们既需要遍历索引同时需要遍历元素的时候,可以考虑使用enumerate函数,enumerate函数接受一个可遍历的对...

python 获取毫秒数,计算调用时长的方法

如题:在python的函数调用中需要记录时间,下面是记录毫秒时间的方法。 import datetime import time t1 = datetime.datetime.now...

opencv设置采集视频分辨率方式

如下所示: #include <opencv2\opencv.hpp> #include<ctime> using namespace cv; usi...

python通过BF算法实现关键词匹配的方法

本文实例讲述了python通过BF算法实现关键词匹配的方法。分享给大家供大家参考。具体实现方法如下: 复制代码 代码如下:#!/usr/bin/python # -*- coding:...

Python3实现的Mysql数据库操作封装类

本文实例讲述了Python3实现的Mysql数据库操作封装类。分享给大家供大家参考,具体如下: #encoding:utf-8 #name:mod_db.py ''''' 使用方法:...