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>The garbage collector would be activated at some point, but you cannot be certain of when unless you force it.</p>

<p>The best way to ensure that the file is closed when you go out of scope just do this:</p>

<pre><code>with open("filename") as f: text = f.read()
</code></pre>

<p>also one-liner but safer.</p>

python file io