title: Python- Writing text to a file?
body: <p>I am trying to learn python and wanted to write some text to a file. I came across two kind of file objects.</p>
  
  <p>fout=open("abc.txt",a)</p>
  
  <p>with open("abc.txt",a) as fout:</p>
  
  <p>The following code:</p>
  
  <pre><code>f= open("abc.txt", 'a')
  f.write("Step 1\n")
  print "Step 1"
  with open("abc.txt", 'a') as fout:
     fout.write("Step 2\n")
  </code></pre>
  
  <p>Gave the output:</p>
  
  <pre><code>Step 2
  Step 1
  </code></pre>
  
  <p>And the following code:</p>
  
  <pre><code>f= open("abc1.txt", 'a')
  f.write("Step 1\n")
  f= open("abc1.txt", 'a')
  f.write("Step 2\n")
  </code></pre>
  
  <p>Gave the output:</p>
  
  <pre><code>Step 1
  Step 2
  </code></pre>
  
  <p>Why is there difference in the outputs? </p>