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


相关文章

使用相同的Apache实例来运行Django和Media文件

Django本身不用来服务media文件;应该把这项工作留给你选择的网络服务器。 我们推荐使用一个单独的网络服务器(即没有运行Django的一个)来服务media。 想了解更多信息,看下...

python去除空格和换行符的实现方法(推荐)

一、去除空格 strip() "   xyz   ".strip()      &nb...

利用pandas将numpy数组导出生成excel的实例

利用pandas将numpy数组导出生成excel的实例

上图 代码 # -*- coding: utf-8 -*- """ Created on Sun Jun 18 20:57:34 2017 @author: Bruce Lau...

python实现树的深度优先遍历与广度优先遍历详解

python实现树的深度优先遍历与广度优先遍历详解

本文实例讲述了python实现树的深度优先遍历与广度优先遍历。分享给大家供大家参考,具体如下: 广度优先(层次遍历) 从树的root开始,从上到下从左到右遍历整个树的节点 数和二叉树的...

在python中获取div的文本内容并和想定结果进行对比详解

div的内容为: <div style="background-color: rgb(255, 238, 221);" id="status" class="errors">...