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 实现微信公众号粉丝迁移流程

近日,因公司业务需要,需将原两个公众号合并为一个,即要将其中一个公众号(主要是粉丝)迁移到另一个公众号。按微信规范,同一用户在不同公众号内的 openid 是不同的,我们的业务系统不例外...

Python适配器模式代码实现解析

Python适配器模式,代码,思考等 # -*- coding: utf-8 -*- # author:baoshan class Computer: def __init__(...

python matplotlib中文显示参数设置解析

python matplotlib中文显示参数设置解析

最近在学习python著名的绘图包matplotlib时发现,有时候图例等设置无法正常显示中文,于是就想把这个问题解决了。 PS:本文仅针对Windows,其他平台仅供参考。 原因 大致...

Python运维自动化之nginx配置文件对比操作示例

Python运维自动化之nginx配置文件对比操作示例

本文实例讲述了Python运维自动化之nginx配置文件对比操作。分享给大家供大家参考,具体如下: 文件差异对比diff.py #!/usr/bin/env python # imp...

python 实现手机自动拨打电话的方法(通话压力测试)

现在能用自动化实现的,尽量使用自动化程序去操作,代替人工去操作,更有效率。 今天说下用python结合adb命令去实现安卓手机端的通话压力测试。 #操作前先在设置里打开power键可...