title: Confusion about with statement python
body: <p>I have a question regarding the use of <code>with</code> statements in python, as given below:</p>
  <pre><code>with open(fname) as f:
      np.save(f,MyData)      
  </code></pre>
  <p>If I'm not mistaken this opens the file <code>fname</code> in a secure manner, such that if an exception occurs the file is closed properly. then it writes <code>MyData</code> to the file. But what I would do is simply:</p>
  <pre><code>np.save(fname,MyData)
  </code></pre>
  <p>This would result in the same, <code>MyData</code> gets written to <code>fname</code>. I'm not sure I understand correctly why the former is better. I don't understand how  this one-liner could keep the file &quot;open&quot; after it has ran the line. Therefore I also don't see how this could create issues when my code would crash afterwards.</p>
  <p>Maybe this is a stupid/basic question, but I always thought that a cleaner code is a nicer code, so not having the extra <code>with</code>-loop seems just better to  me.</p>