title: Python, difference between 'open' and 'with open'
body: <p>I have not used the <code>with</code> statement, but am somewhat familiar with its purpose. With the follow code, the <code>#1</code> block works as expected, but <code>#2</code> -- which, correct me here, should do the same thing as the first one -- throws the following exception <code>FileExistsError: [Errno 17] File exists: 'mydir'</code>.</p>
  
  <pre><code>import os
  
  if not(os.path.exists('mydir')):
      os.makedirs('mydir')
  
  path = 'mydir'
  filename = 'msg.txt'
  filename2 = 'msg2.txt'
  
  #1
  with open(os.path.join(path, filename), 'w') as temp_file:
      temp_file.write("hello")
  
  #2
  temp_file = open(os.path.join(path, filename2), 'w')
  temp_file.write("hello again")
  temp_file.close()   
  </code></pre>