擅长:python、mysql、java
<p>试试看,这样更好一点,占用的内存更少,而且您不需要在函数中创建列表或加载所有文件内容:</p>
<pre><code>import zipfile
import os
def out_fun():
for x in os.listdir('C:\Users\Guest\Desktop\OJT\scanner\samples_raw'):
yield x
file = open("result_vsdt.txt","w")
for line in out_fun():
file.write(line + '\n')
file.close()
</code></pre>
<p>编辑:
<br/>
打开文件时最好使用<code>with</code>,如下所示:</p>
<pre><code>with open("result_vsdt.txt","w") as file:
for line in out_fun():
file.write(line + '\n')
</code></pre>
<p>它将自动关闭文件。你知道吗</p>