在Python中使用next()方法操作文件的教程


Posted in Python onMay 24, 2015

 next()方法当一个文件被用作迭代器,典型例子是在一个循环中被使用,next()方法被反复调用。此方法返回下一个输入行,或引发StopIteration异常EOF时被命中。

与其它文件的方法,如ReadLine()相结合next()方法工作不正常。然而,usingseek()将文件重新定位到一个绝对位置将刷新预读缓冲器。
语法

以下是next()方法的语法:

fileObject.next();

参数

  •     NA

返回值

此方法返回下一个输入行。
例子

下面的示例演示next()方法的使用。

#!/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

for index in range(5):
  line = fo.next()
  print "Line No %d - %s" % (index, line)

# Close opend file
fo.close()

当我们运行上面的程序,它会产生以下结果:

Name of the file: foo.txt
Line No 0 - This is 1st line

Line No 1 - This is 2nd line

Line No 2 - This is 3rd line

Line No 3 - This is 4th line

Line No 4 - This is 5th line
Python 相关文章推荐
python练习程序批量修改文件名
Jan 16 Python
Django中实现一个高性能计数器(Counter)实例
Jul 09 Python
web.py中调用文件夹内模板的方法
Aug 26 Python
17个Python小技巧分享
Jan 23 Python
python友情链接检查方法
Jul 08 Python
Python实现的单向循环链表功能示例
Nov 10 Python
说说如何遍历Python列表的方法示例
Feb 11 Python
Django admin禁用编辑链接和添加删除操作详解
Nov 15 Python
在Pytorch中计算卷积方法的区别详解(conv2d的区别)
Jan 03 Python
Python实现获取当前目录下文件名代码详解
Mar 10 Python
python如何进入交互模式
Jul 06 Python
sublime3之内网安装python插件Anaconda的流程
Nov 10 Python
在Python程序中操作文件之isatty()方法的使用教程
May 24 #Python
讲解Python中fileno()方法的使用
May 24 #Python
在Python程序中操作文件之flush()方法的使用教程
May 24 #Python
Python编程中用close()方法关闭文件的教程
May 24 #Python
详细讲解Python中的文件I/O操作
May 24 #Python
详解在Python中处理异常的教程
May 24 #Python
Python中关于使用模块的基础知识
May 24 #Python
You might like
php Try Catch异常测试
2009/03/01 PHP
PHP 第二节 数据类型之字符串类型
2012/04/28 PHP
php使用date和strtotime函数输出指定日期的方法
2014/11/14 PHP
php获得文件大小和文件创建时间的方法
2015/03/13 PHP
有关PHP 中 config.m4 的探索
2020/08/26 PHP
基于JQuery的一个简单的鼠标跟随提示效果
2010/09/23 Javascript
结合JQ1.9通过js正则判断各种浏览器版本的方法
2013/12/30 Javascript
浅谈jQuery中hide和fadeOut的区别 show和fadeIn的区别
2016/08/18 Javascript
总结Javascript中数组各种去重的方法
2016/10/04 Javascript
jQuery日程管理插件fullcalendar使用详解
2017/01/07 Javascript
学好js,这些js函数概念一定要知道【推荐】
2017/01/19 Javascript
angular forEach方法遍历源码解读
2017/01/25 Javascript
微信小程序日历组件calendar详解及实例
2017/06/08 Javascript
前端常见跨域解决方案(全)
2017/09/19 Javascript
vux-scroller实现移动端上拉加载功能过程解析
2019/10/08 Javascript
VUEX采坑之路之获取不到$store的解决方法
2019/11/08 Javascript
vue穿梭框实现上下移动
2021/01/29 Vue.js
[01:08]DOTA2次级职业联赛 - Wings 战队宣传片
2014/12/01 DOTA
[00:12]DAC2018 天才少年转战三号位,他的SOLO是否仍如昔日般强大?
2018/04/06 DOTA
python使用socket向客户端发送数据的方法
2015/04/29 Python
详解Python中for循环是如何工作的
2017/06/30 Python
Python实现简单的语音识别系统
2017/12/13 Python
django admin后台添加导出excel功能示例代码
2019/05/15 Python
python切片的步进、添加、连接简单操作示例
2019/07/11 Python
Python 实现的 Google 批量翻译功能
2019/08/26 Python
tensorflow入门:TFRecordDataset变长数据的batch读取详解
2020/01/20 Python
浅谈tensorflow 中tf.concat()的使用
2020/02/07 Python
学python需要去培训机构吗
2020/07/01 Python
Python Opencv实现单目标检测的示例代码
2020/09/08 Python
Django返回HTML文件的实现方法
2020/09/17 Python
亚马逊西班牙购物网站:amazon西班牙
2017/03/06 全球购物
远东集团网络工程师面试题
2014/10/20 面试题
护士自我介绍信
2014/01/13 职场文书
优秀毕业自我鉴定
2014/02/15 职场文书
2014年大班元旦活动方案
2014/02/26 职场文书
农村党员干部承诺书
2015/05/04 职场文书