Is file automatically closed if read in same line as opening?
<p>If I do (in Python):</p>

<pre><code>text = open("filename").read()
</code></pre>

<p>is the file automatically closed?</p>

<p>Since you have no reference on the open file handle,
CPython will close it automatically either during garbage collection or at program exit.  The problem here is that you don't have any guarantees about when that will occur, which is why the <code>with open(...)</code> construct is preferred.  </p>

python file io