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


相关文章

关于tensorflow的几种参数初始化方法小结

在tensorflow中,经常会遇到参数初始化问题,比如在训练自己的词向量时,需要对原始的embeddigs矩阵进行初始化,更一般的,在全连接神经网络中,每层的权值w也需要进行初始化。...

全面了解python字符串和字典

很多序列的方法字符串同样适用, 但是,字符串是不可变的,所以一些试图改变字符串的方法是不可用的 1 字符串格式化 1)用元组或者字典格式化字符串 format = "hello,%s.s...

python中numpy基础学习及进行数组和矢量计算

python中numpy基础学习及进行数组和矢量计算

前言 在python 中有时候我们用数组操作数据可以极大的提升数据的处理效率,类似于R的向量化操作,是的数据的操作趋于简单化,在python 中是使用numpy模块可以进行数组和矢量计算...

python 实现敏感词过滤的方法

如下所示: #!/usr/bin/python2.6 # -*- coding: utf-8 -*- import time class Node(object): d...

使用Python & Flask 实现RESTful Web API的实例

环境安装: sudo pip install flask Flask 是一个Python的微服务的框架,基于Werkzeug, 一个 WSGI 类库。 Flask 优点: Written...