Python中tell()方法的使用详解

yipeiwu_com5年前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实现简单聊天室功能的具体代码,供大家参考,具体内容如下 公共模块 首先写一个公共类,用字典的形式对数据的收发,并且进行封装,导入struct解决了TCP的...

django 发送手机验证码的示例代码

django 发送手机验证码的示例代码

一、流程分析: 1.用户在项目前端,输入手机号,然后点击【获取验证码】,将手机号发到post到后台。 2.后台验证手机号是否合法,是否已被占用,如果通过验证,则生成验证码,并通过运行脚本...

详谈Numpy中数组重塑、合并与拆分方法

1.数组重塑 1.1一维数组转变成二维数组 通过reshape( )函数即可实现,假设data是numpy.array类型的一维数组array([0, 1, 2, 3, 4, 5, 6,...

Python 实现文件打包、上传与校验的方法

不多说,我们直接上源码: # -*- coding:UTF-8 -*- ''' 实现文件打包、上传与校验 Created on 2018年1月12日 @author: liuyazh...