Python中tell()方法的使用详解


Posted in Python onMay 24, 2015

 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 相关文章推荐
python3实现TCP协议的简单服务器和客户端案例(分享)
Jun 14 Python
pandas 获取季度,月度,年度首尾日期的方法
Apr 11 Python
Python socket实现多对多全双工通信的方法
Feb 13 Python
pandas的to_datetime时间转换使用及学习心得
Aug 11 Python
Python文件操作函数用法实例详解
Dec 24 Python
keras模型可视化,层可视化及kernel可视化实例
Jan 24 Python
完美解决keras保存好的model不能成功加载问题
Jun 11 Python
django rest framework 自定义返回方式
Jul 12 Python
python3.7.3版本和django2.2.3版本是否可以兼容
Sep 01 Python
深入浅析pycharm中 Make available to all projects的含义
Sep 15 Python
python 实现关联规则算法Apriori的示例
Sep 30 Python
Python实现图片指定位置加图片水印(附Pyinstaller打包exe)
Mar 04 Python
在Python中操作文件之seek()方法的使用教程
May 24 #Python
简单介绍Python中的readline()方法的使用
May 24 #Python
在Python中操作文件之read()方法的使用教程
May 24 #Python
在Python中使用next()方法操作文件的教程
May 24 #Python
在Python程序中操作文件之isatty()方法的使用教程
May 24 #Python
讲解Python中fileno()方法的使用
May 24 #Python
在Python程序中操作文件之flush()方法的使用教程
May 24 #Python
You might like
新闻分类录入、显示系统
2006/10/09 PHP
php array_intersect()函数使用代码
2009/01/14 PHP
Laravel框架表单验证详解
2014/09/04 PHP
Yii2配置Nginx伪静态的方法
2017/05/05 PHP
比较简单实用的使用正则三种版本的js去空格处理方法
2007/11/18 Javascript
写出更好的JavaScript之undefined篇(上)
2009/11/22 Javascript
JS幻灯片可循环播放可平滑旋转带滚动导航(自写)
2013/08/05 Javascript
jquery设置元素的readonly和disabled的写法
2013/09/22 Javascript
Bootstrap对话框使用实例讲解
2016/09/24 Javascript
AngularJS 实现点击按钮获取验证码功能实例代码
2017/07/13 Javascript
Vue通过URL传参如何控制全局console.log的开关详解
2017/12/07 Javascript
angular2实现统一的http请求头方法
2018/08/13 Javascript
Node.js的进程管理的深入理解
2019/01/09 Javascript
Vue2(三)实现子菜单展开收缩,带动画效果实现方法
2019/04/28 Javascript
Vue动态生成表格的行和列
2019/07/18 Javascript
谈谈JavaScript中的函数
2020/09/08 Javascript
jQuery实现回到顶部效果
2020/10/19 jQuery
[01:02]2014 DOTA2国际邀请赛中国区预选赛 现场抢先看
2014/05/22 DOTA
python计数排序和基数排序算法实例
2014/04/25 Python
利用python求相邻数的方法示例
2017/08/18 Python
python面试题小结附答案实例代码
2019/04/11 Python
解决python flask中config配置管理的问题
2019/07/26 Python
python 的 openpyxl模块 读取 Excel文件的方法
2019/09/09 Python
基于Python检测动态物体颜色过程解析
2019/12/04 Python
Python3操作MongoDB增册改查等方法详解
2020/02/10 Python
opencv 形态学变换(开运算,闭运算,梯度运算)
2020/07/07 Python
CSS3色彩模式有哪些?CSS3 HSL色彩模式的定义
2016/04/26 HTML / CSS
iostream与iostream.h的区别
2015/01/16 面试题
Ruby如何定义一个类
2012/10/08 面试题
电信专业应届生自荐信
2013/09/28 职场文书
中层干部竞争上岗演讲稿
2014/01/13 职场文书
学生干部的自我评价分享
2014/01/18 职场文书
小松树教学反思
2014/02/11 职场文书
外语系大学生自荐信范文
2014/03/01 职场文书
班级旅游计划书
2014/05/03 职场文书
祖国在我心中演讲稿450字
2014/09/05 职场文书