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 相关文章推荐
跟老齐学Python之折腾一下目录
Oct 24 Python
Python使用PIL库实现验证码图片的方法
Mar 11 Python
python3实现随机数
Jun 25 Python
详解Python是如何实现issubclass的
Jul 24 Python
Python迷宫生成和迷宫破解算法实例
Dec 24 Python
python3注册全局热键的实现
Mar 22 Python
Python模拟登入的N种方式(建议收藏)
May 31 Python
python Tornado框架的使用示例
Oct 19 Python
python实现测试工具(一)——命令行发送get请求
Oct 19 Python
接口自动化多层嵌套json数据处理代码实例
Nov 20 Python
Python实现文本文件拆分写入到多个文本文件的方法
Apr 18 Python
使用pipenv管理python虚拟环境的全过程
Sep 25 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
php递归获取目录内文件(包含子目录)封装类分享
2013/12/25 PHP
PHP采集静态页面并把页面css,img,js保存的方法
2014/12/23 PHP
php使用curl实现简单模拟提交表单功能
2017/05/15 PHP
laravel 解决路由除了根目录其他都404的问题
2019/10/18 PHP
PHP 进程池与轮询调度算法实现多任务的示例代码
2019/11/26 PHP
完整显示当前日期和时间的JS代码
2007/09/17 Javascript
jquery.pagination.js 无刷新分页实现步骤分享
2012/05/23 Javascript
Javascript 按位取反运算符 (~)
2014/02/04 Javascript
js replace替换所有匹配的字符串
2014/02/13 Javascript
JavaSciprt中处理字符串之sup()方法的使用教程
2015/06/08 Javascript
微信小程序 教程之wxapp 视图容器 view
2016/10/19 Javascript
js获取json中key所对应的value值的简单方法
2020/06/17 Javascript
vue Element-ui input 远程搜索与修改建议显示模版的示例代码
2017/10/19 Javascript
vue定义全局变量和全局方法的方法示例
2018/08/01 Javascript
用POSTMAN发送JSON格式的POST请求示例
2018/09/04 Javascript
nodejs log4js 使用详解
2019/05/31 NodeJs
vue项目出现页面空白的解决方案
2019/10/31 Javascript
Python中的特殊语法:filter、map、reduce、lambda介绍
2015/04/14 Python
Python用threading实现多线程详解
2017/02/03 Python
Python实现将16进制字符串转化为ascii字符的方法分析
2017/07/21 Python
opencv实现静态手势识别 opencv实现剪刀石头布游戏
2019/01/22 Python
pip安装提示Twisted错误问题(Python3.6.4安装Twisted错误)
2020/05/09 Python
日本食品网上商店:JaponShop.com
2017/11/28 全球购物
顶丰TOPPIK台湾官网:增发纤维假发,告别秃发困扰
2018/06/13 全球购物
Craghoppers德国官网:户外和旅行服装
2020/02/14 全球购物
初始化了一个没有run()方法的线程类,是否会出错?
2014/03/27 面试题
青春无悔演讲稿
2014/05/08 职场文书
淘宝店策划方案
2014/06/07 职场文书
2015年银行员工工作总结
2015/04/24 职场文书
2015年幼儿园个人工作总结
2015/04/25 职场文书
2015年招生工作总结
2015/05/04 职场文书
健身房被搭讪?用python写了个小米计时器助人为乐
2021/06/08 Python
Python基于百度AI实现抓取表情包
2021/06/27 Python
MySQL系列之十五 MySQL常用配置和性能压力测试
2021/07/02 MySQL
关于python3 opencv 图像二值化的问题(cv2.adaptiveThreshold函数)
2022/04/04 Python
Vue深入理解插槽slot的使用
2022/08/05 Vue.js