擅长:python、mysql、java
<h2>为什么只将最后一个文件的名称写入结果文件?你知道吗</h2>
<p>因为当<code>x</code>的每个值都在<code>out_fun</code>函数中打印时,实际上只返回最后一个值。你需要把其他的放在某个地方,然后把它们放回原处。你知道吗</p>
<h2>如何返回所有内容?你知道吗</h2>
<p>创建一个名为<code>output</code>的字符串,将<code>x</code>的每个值附加到该字符串上,然后返回:</p>
<pre><code>import os
def out_fun():
output = ''
for x in os.listdir('C:\Users\Guest\Desktop\OJT\scanner\samples_raw'):
print x
output += x + '\n'
return output
with file = open("result_vsdt.txt","w"):
file.write(out_fun())
</code></pre>
<p><code>'\n'</code>是新行字符。这使得舒尔,每一个文件名都在一条新的线上。你知道吗</p>