小白教程
所有教程
关于
Search
172.70.178.108
172.70.178.108
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Python3 file next”的源代码
本页内容
上一节:
Python3_file_isatty
下一节:
Python3_file_read
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:file.next()}}[[Category:python3 file methods|5]] = Python3 File next() 方法 = [[python3 file methods|Python3 File(文件) 方法]] === 概述 === '''Python 3 中的 File 对象不支持 next() 方法。''' Python 3 的内置函数 next() 通过迭代器调用 __next__() 方法返回下一项。 在循环中,next()方法会在每次循环中调用,该方法返回文件的下一行,如果到达结尾(EOF),则触发 StopIteration === 语法 === next() 方法语法如下: <sample title="" desc="" lang="python" hererun="1"> next(iterator[,default]) </sample> === 参数 === * '''无''' === 返回值 === 返回文件下一行。 == 以下实例演示了 next() 方法的使用: 文件 xiaobai.txt 的内容如下: <sample title="" desc="" lang="python" hererun="1"> 这是第一行 这是第二行 这是第三行 这是第四行 这是第五行 </sample> 循环读取文件的内容: <sample title="" desc="" lang="python" hererun="1"> #!/usr/bin/python3 # 打开文件 fo = open("xiaobai.txt", "r") print ("文件名为: ", fo.name) for index in range(5): line = next(fo) print ("第 %d 行 - %s" % (index, line)) # 关闭文件 fo.close() </sample> 以上实例输出结果为: <sample title="" desc="" lang="python" hererun="1"> 文件名为: xiaobai.txt 第 0 行 - 这是第一行 第 1 行 - 这是第二行 第 2 行 - 这是第三行 第 3 行 - 这是第四行 第 4 行 - 这是第五行 </sample> [[python3 file methods|Python3 File(文件) 方法]]
返回至“
Python3 file next
”。
上一节:
Python3_file_isatty
下一节:
Python3_file_read