在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生成器(Generator)详解
Apr 13 Python
利用Python脚本在Nginx和uwsgi上部署MoinMoin的教程
May 05 Python
Python爬虫爬验证码实现功能详解
Apr 14 Python
Python中函数eval和ast.literal_eval的区别详解
Aug 10 Python
11月编程语言排行榜 Python逆袭C#上升到第4
Nov 15 Python
python使用Tkinter实现在线音乐播放器
Jan 30 Python
python3库numpy数组属性的查看方法
Apr 17 Python
PyCharm的设置方法和第一个Python程序的建立
Jan 16 Python
Python 操作mysql数据库查询之fetchone(), fetchmany(), fetchall()用法示例
Oct 17 Python
python itsdangerous模块的具体使用方法
Feb 17 Python
Python实现打包成库供别的模块调用
Jul 13 Python
python线程优先级队列知识点总结
Feb 28 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
关于使用key/value数据库redis和TTSERVER的心得体会
2013/06/28 PHP
thinkPHP中钩子的使用方法实例分析
2017/11/16 PHP
用javascript连接access数据库的方法
2006/11/17 Javascript
删除重复数据的算法
2006/11/23 Javascript
js防止表单重复提交实现代码
2012/09/05 Javascript
js禁止页面使用右键(简单示例代码)
2013/11/13 Javascript
jQuery使用andSelf()来包含之前的选择集
2014/05/19 Javascript
在Node.js中实现文件复制的方法和实例
2014/06/05 Javascript
JavaScript获取鼠标移动时的坐标(兼容IE8、chome谷歌、Firefox)
2014/09/13 Javascript
jQuery中:empty选择器用法实例
2014/12/30 Javascript
JS+CSS实现模仿浏览器网页字符查找功能的方法
2015/02/26 Javascript
JQuery限制复选框checkbox可选中个数的方法
2015/04/20 Javascript
基于Jquery实现万圣节快乐特效
2015/11/01 Javascript
详解JavaScript 中的 replace 方法
2016/01/01 Javascript
jQuery简易时光轴实现方法示例
2017/03/13 Javascript
详解Vue的组件中data选项为什么必须是函数
2020/08/17 Javascript
Python实现发送email的几种常用方法
2014/08/18 Python
python自动化测试之setUp与tearDown实例
2014/09/28 Python
python矩阵的转置和逆转实例
2018/12/12 Python
python 视频逐帧保存为图片的完整实例
2019/12/10 Python
pycharm运行程序时看不到任何结果显示的解决
2020/02/21 Python
Python基础类继承重写实现原理解析
2020/04/03 Python
利用keras使用神经网络预测销量操作
2020/07/07 Python
中兴通讯全球官方网站:ZTE
2020/12/26 全球购物
汽车专业人才自我鉴定范文
2013/12/29 职场文书
三年级科学教学反思
2014/01/29 职场文书
大学考试作弊检讨书
2014/01/30 职场文书
数控技术应用个人求职信范文
2014/02/03 职场文书
户外宣传策划方案
2014/05/25 职场文书
2015年初中元旦晚会活动总结
2014/11/28 职场文书
先进典型事迹材料
2014/12/29 职场文书
二审代理词范文
2015/05/25 职场文书
培养联系人考察意见
2015/06/01 职场文书
MySQL之select、distinct、limit的使用
2021/11/11 MySQL
使用CSS连接数据库的方式
2022/02/28 HTML / CSS
PyTorch中的torch.cat简单介绍
2022/03/17 Python