答案 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
可确保在使用后清除文件,即使发生异常也是如此。