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第八周:详解网络编程基础(socket)

一,Socket编程 (1)Socket方法介绍 Socket是网络编程的一个抽象概念。通常我们用一个Socket表示“打开了一个网络链接“,而打开一个Socket需要知道目标计...

tornado+celery的简单使用详解

celery是实现一个简单,灵活可靠的分布式任务队列系统的好选择 tornado则不用过多介绍 在开发机上安装rabbitmq这里就不介绍了 首先是task文件的编写 task.py...

python实现二叉查找树实例代码

本文研究的主要是python实现二叉查找树的相关内容,具体介绍及实现如下。 1. 二叉查找树的定义: 左子树不为空的时候,左子树的结点值小于根节点,右子树不为空时,右子树的结点值大于根节...

Python上下文管理器和with块详解

上下文管理器和with块,具体内容如下 上下文管理器对象存在的目的是管理 with 语句,就像迭代器的存在是为了管理 for 语句一样。 with 语句的目的是简化 try/finall...

使用Python监视指定目录下文件变更的方法

监视指定目录下文件变更。 # -*- coding: utf-8 -*- # @Author: xiaodong # @Date: just hide # @Last Modifie...