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


相关文章

Python 条件判断的缩写方法

return (1==1) ? "is easy" : "my god" //C...

关于Python形参打包与解包小技巧分享

Python中的函数调用与c++不同的是将this指针直接作为self当作第一个形参进行处理,从而将静态函数与实例方法的调用形式统一了起来。在实际编程过程中,可以通过传递函数的地址、函数...

python+requests+unittest API接口测试实例(详解)

python+requests+unittest API接口测试实例(详解)

我在网上查找了下接口测试相关的资料,大都重点是以数据驱动的形式,将用例维护在文本或表格中,而没有说明怎么样去生成想要的用例, 问题: 测试接口时,比如参数a,b,c,我要先测a参数,有(...

Python实现CET查分的方法

Python CET自动查询方法需要用到的python方法模块有:sys、urllib2 本文实例讲述了Python实现CET查分的方法。分享给大家供大家参考。具体实现方法如下: 复制代...

pyQT5 实现窗体之间传值的示例

pyQT5 实现窗体之间传值的示例

准备 一个MainWindow和一个WidgetForm,总代码如下 # -*- coding: utf-8 -*- from PyQt5 import QtWidgets fr...