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


相关文章

pyqt和pyside开发图形化界面

复制代码 代码如下:#!/usr/bin/env pythonimport sysfrom PyQt4 import QtGui,QtCoreimport httplibfrom url...

pytorch动态网络以及权重共享实例

pytorch 动态网络+权值共享 pytorch以动态图著称,下面以一个栗子来实现动态网络和权值共享技术: # -*- coding: utf-8 -*- import rando...

对python字典过滤条件的实例详解

如下所示: d = { 'a': '0.0000', 'b': '1.2' } d_tmp = dict((key, value) for key, value in d...

Python进阶之尾递归的用法实例

作者是一名沉迷于Python无法自拔的蛇友,为提高水平,把Python的重点和有趣的实例发在简书上。 尾递归 如果一个函数中所有递归形式的调用都出现在函数的末尾,我们称这个递归函数是尾...

python 实现提取log文件中的关键句子,并进行统计分析

利用python开发了一个提取sim.log 中的各个关键步骤中的时间并进行统计的程序: #!/usr/bin/python2.6 import re,datetime file_n...