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的urllib模块显示下载进度示例

复制代码 代码如下: def report_hook(count, block_size, total_size):...     pr...

Django restframework 框架认证、权限、限流用法示例

本文实例讲述了Django restframework 框架认证、权限、限流用法。分享给大家供大家参考,具体如下: 概述 Django Rest Framework 是一个强大且灵活的工...

利用python numpy+matplotlib绘制股票k线图的方法

利用python numpy+matplotlib绘制股票k线图的方法

一、python numpy + matplotlib 画股票k线图 # -- coding: utf-8 -- import requests import numpy as np...

Python性能分析工具Profile使用实例

这篇文章主要介绍了Python性能分析工具Profile使用实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 代码优化的前提是需要了...

pandas 获取季度,月度,年度首尾日期的方法

可实现类似于sql中的dateadd、datesub的功能 两种获取日期的方式 z=datetime.datetime(2016,12,5) z=datetime.datetime....