迭代器的元素就是一行字符串

代码

1
2
3
4
5
6
7
with open('test.txt') as f:
while True:
try:
print(next(f)) # print(f.__next__())一样的效果
except StopIteration:
break
print('end')

参考链接:Python循环列表、可迭代对象判断是否最后一次循环,iter,__next__实现

创建于2023.2.13/22.25,修改于2023.2.13/22.25