title: Is explicitly closing files important?
body: <p>In Python, if you either open a file without calling <code>close()</code>, or close the file but not using <code>try</code>-<code>finally</code> or the "<code>with</code>" statement, is this a problem?  Or does it suffice as a coding practice to rely on the Python garbage-collection to close all files?  For example, if one does this:</p>
  
  <pre><code>for line in open("filename"):
      # ... do stuff ...
  </code></pre>
  
  <p>... is this a problem because the file can never be closed and an exception could occur that prevents it from being closed?  Or will it definitely be closed at the conclusion of the <code>for</code> statement because the file goes out of scope?</p>