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 旋转打印各种矩形的方法

打印旋转矩阵应该是很经典的算法问题了。 题目描述如下: 给定一个m * n要素的矩阵。按照螺旋顺序,返回该矩阵的所有要素。 思路:1,先定义矩阵的左上和右下的坐标,然后通过两个坐标来打印...

对python中的logger模块全面讲解

logging模块介绍 Python的logging模块提供了通用的日志系统,熟练使用logging模块可以方便开发者开发第三方模块或者是自己的Python应用。同样这个模块提供不同的日...

PyQt5 对图片进行缩放的实例

如下所示: def shrinkImage(self): ''' 缩小图片 :return: ''' scale = 0.8 #每次缩小20% img = QImage...

Python 新建文件夹与复制文件夹内所有内容的方法

在指定路径下新建一个文件夹: import os def newfile(path): path=path.strip() path=path.rstrip("\\") # 判...