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)

fo.close()

输出结果:

文件名: test.txt

0 one
1 two
2 three
3 four
4 five
原文地址:https://www.cnblogs.com/keye/p/15464367.html