Files
python3-cookbook/cookbook/c04/p01_manual_iterator.py

28 lines
532 B
Python
Raw Permalink Normal View History

2014-09-14 12:12:44 +08:00
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
Topic: 手动遍历迭代器
Desc :
"""
def manual_iter():
with open('/etc/passwd') as f:
try:
while True:
line = next(f)
print(line, end='')
except StopIteration:
pass
def manual_iter2():
with open('/etc/passwd') as f:
while True:
line = next(f)
if line is None:
break
print(line, end='')
if __name__ == '__main__':
manual_iter()