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中的turtle模块画图两只小羊方法

用Python中的turtle模块画图两只小羊方法

这两天在一个公众号里看到好玩的turtle库,今天来学习一下。 turtle.circle(radius, extent=None, steps=None) 描述: 以给定半径画圆 参数...

python获取指定网页上所有超链接的方法

本文实例讲述了python获取指定网页上所有超链接的方法。分享给大家供大家参考。具体如下: 这段python代码通过urllib2抓取网页,然后通过简单的正则表达式分析网页上的全部url...

python通过get,post方式发送http请求和接收http响应的方法

本文实例讲述了python通过get,post方式发送http请求和接收http响应的方法。分享给大家供大家参考。具体如下: 测试用CGI,名字为test.py,放在apache的cgi...

python实现机器人行走效果

本文实例为大家分享了python实现机器人行走效果的具体代码,供大家参考,具体内容如下 #! /usr/bin/env python3 # -*- coding: utf-8 -*...

windows下python安装paramiko模块和pycrypto模块(简单三步)

前言 Python中使用SSH需要用到OpenSSH,而OpenSSH依赖于paramiko模块,而paramiko模块又依赖于pycrypto模块,因此要在python中使用SSH,我...