时间:2010-07-23 21:15:12

标签: python iterator

3 个答案:

答案 0 :(得分:36)

答案 1 :(得分:9)

答案 2 :(得分:3)

后人的完整示例:

from itertools import izip, izip_longest

file1name = '/some/path/and/file'
file2name = '/another/path/and/another/file'

with open(file1name) as file1, open(file2name) as file2:
    for line1, line2 in izip(file1, file2):   # or izip_longest
        # do something with the lines

使用with可确保在使用后清除文件,即使发生异常也是如此。