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综合应用名片管理系统案例详解

Python综合应用名片管理系统案例详解

本文实例讲述了Python综合应用名片管理系统。分享给大家供大家参考,具体如下: 综合应用已经学习过的知识点: 变量 流程控制 函数 模块 开发 名片管理系统 系统需...

Python实现的字典值比较功能示例

Python实现的字典值比较功能示例

本文实例讲述了Python实现的字典值比较功能。分享给大家供大家参考,具体如下: #coding=utf8 import logging import os from Lib.Dea...

Python的“二维”字典 (two-dimension dictionary)定义与实现方法

本文实例讲述了Python的“二维”字典 (two-dimension dictionary)定义与实现方法。分享给大家供大家参考,具体如下: Python 中的dict可以实现迅速查找...

基于python实现学生管理系统

本文为大家分享了python实现学生管理系统的具体代码,供大家参考,具体内容如下 1.0版本学生管理系统 ''' 1.添加学员 2.修改学员 3.查询学员 4.删除学员 0...

python远程邮件控制电脑升级版

由于前边Python3.4实现远程控制电脑开关机写的远程操控电脑,使用的POP登陆有使用频率限制,导致非常被动,有时候邮件无法读取,下面改用POST网易邮箱的方法,获取邮件 impo...