擅长:python、mysql、java
<p>您没有正确关闭文件。要关闭它,您缺少<code>()</code>末尾的<code>file.close</code>,因此它应该如下所示:</p>
<pre><code>file = open("BAL.txt", "w")
file.write("This has been written to a file")
file.close()
</code></pre>
<p><a href="https://programminghistorian.org/lessons/working-with-text-files" rel="nofollow noreferrer">This site has the same example and may be of some use to you.</a></p>
<hr/>
<p>另一种方法,在将多个值附加到单个文件中时特别有用,就是使用<code>with open("BAL.txt","w") as file:</code>之类的方法。下面是重写的脚本,其中包括以下示例:</p>
<pre><code>I = '200'
with open("BAL.txt","w") as file:
file.write(I)
</code></pre>
<p>这将用值<code>file</code>打开我们的文件,并允许我们向其中写入值。还要注意,这里不需要<code>file.close()</code>,在追加文本时需要使用<code>w+</code>。你知道吗</p>