擅长:python、mysql、java
<p>试着这样做:</p>
<pre><code>with open("database.txt", "w") as f:
f.write("Hello!")
with open("database.txt", "r") as f:
for line in f:
print line
</code></pre>
<p>您可以避免使用<a href="https://docs.python.org/2/reference/compound_stmts.html#with" rel="nofollow noreferrer">with statement</a>手动关闭文件。
另外,一旦你写了文件,你就可以阅读它。
一般来说,最好将对文件的读写访问分开。除非你真的需要两者兼而有之。你知道吗</p>