擅长:python、mysql、java
<p>后人的一个完整例子:</p>
<pre><code>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
</code></pre>
<p>使用<code>with</code>确保文件在使用后被清除,即使发生异常。</p>