擅长:python、mysql、java
<p>您使用上下文管理器<code>with</code>,这意味着当您退出with作用域时,文件将被关闭。所以<code>outfile</code>在你使用它的时候显然是关闭的。你知道吗</p>
<pre><code>with open('path\\to\\dir\\file.txt', 'w') as outfile:
for fname in filenames:
nb = nb + 1
print fname
print nb
currentfile = os.path.join(dirpath, fname)
with open(currentfile) as infile:
for line in infile:
outfile.write(line)
</code></pre>