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递归函数实例 1、打开Python开发工具IDLE,新建‘递归.py'文件,并写代码如下: def digui(n): if n == 0 : print...

Python中的 is 和 == 以及字符串驻留机制详解

is 和 == 先了解下官方文档中关于 is 和 == 的概念。is 表示的是对象标示符(object identity),而 == 表示的是相等(equality);is 的作用是用...

Python基于PycURL自动处理cookie的方法

本文实例讲述了Python基于PycURL自动处理cookie的方法。分享给大家供大家参考。具体如下: import pycurl import StringIO url = "ht...

对Python中的@classmethod用法详解

在Python面向对象编程中的类构建中,有时候会遇到@classmethod的用法。 总感觉有这种特殊性说明的用法都是高级用法,在我这个层级的水平中一般是用不到的。 不过还是好奇去查了一...

python 输出列表元素实例(以空格/逗号为分隔符)

给定list,如何以空格/逗号等符号以分隔符输出呢? 一般的,简单的for循环可以打印出list的内容: l=[1,2,3,4] for i in l: print(i) 输出结...