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


相关文章

Pycharm学习教程(6) Pycharm作为Vim编辑器使用

Pycharm学习教程(6) Pycharm作为Vim编辑器使用

Pycharm作为Vim编辑器使用,具体内容如下 1、主题   如果你是Vim的粉丝,并且不打算使用其他类型的编辑器,那么这篇教程将会比较适合你。这里将会详细介绍如何在Pycharm...

详解Python Matplotlib解决绘图X轴值不按数组排序问题

详解Python Matplotlib解决绘图X轴值不按数组排序问题

在用Matplotlib库绘制折线图的时候遇到一个问题,当定义一个x轴数组时,plot绘制折线图时,x轴并不会按照我们定义的数组的顺序去排列显示,例如: import matplot...

Python __setattr__、 __getattr__、 __delattr__、__call__用法示例

getattr `getattr`函数属于内建函数,可以通过函数名称获取 复制代码 代码如下: value = obj.attribute value = getattr(obj, "a...

python pandas 对时间序列文件处理的实例

如下所示: import pandas as pd from numpy import * import matplotlib.pylab as plt import copy d...

浅谈Python中的zip()与*zip()函数详解

前言 1.实验环境: Python 3.6; 2.示例代码地址:下载示例; 3.本文中元素是指列表、元组、字典等集合类数据类型中的下一级项目(可能是单个元素或嵌套列表)。 zi...