Python3 File next()

概述

Python3 的 File 对象不支持 next() 方法。

Python3 的内置函数 next() 通过迭代器调用 __next__() 方法返回下一项。


语法

next(iterator[, default])

返回值

返回文件下一行。


实例

文件内容:

one
two
three
four
five

读取文件:

fo = open('test.txt', 'r')
print('文件名:', fo.name)

for index in range(5):
    line = next(fo)
    print(index, line, end='')

fo.close()

输出结果:

文件名: test.txt
0 one
1 two
2 three
3 four
4 five
posted @ 2021-10-26 09:39  做梦当财神  阅读(171)  评论(0编辑  收藏  举报