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字典操作简明总结

1.dict()创建字典 复制代码 代码如下: >>> fdict = dict((['x', 1], ['y', 2])) >>> fdict {'...

TensorFlow Session会话控制&Variable变量详解

TensorFlow Session会话控制&Variable变量详解

这篇文章主要讲TensorFlow中的Session的用法以及Variable。 Session会话控制 Session是TensorFlow为了控制和输出文件的执行语句,运行sessi...

Python os模块学习笔记

一、os模块概述 Python os模块包含普遍的操作系统功能。例如文件的复制、创建、修改、删除文件及文件夹... 二、常用方法 1、os.listdir()   返...

python学生管理系统学习笔记

本文实例为大家分享了python学生管理系统的具体代码,供大家参考,具体内容如下 基于列表存储的学生管理系统,实现如下功能 ================== 学生管理系统 1、添加...

Python中关于浮点数的冷知识

本周的PyCoder's Weekly 上分享了一篇小文章,它里面提到的冷知识很有意思,我稍作补充,分享给大家。 它提到的部分问题,读者们可以先思考下: 若两个元组相等,即 a==...