title: Writing for loop output to a text file in python
body: <p>I'm new to Python and I'm using it to write FeniCS FEA model fro heat transfer. However, I was able to write code that does what I wanted it to do except to writing thousands of lines that results from a for loop to a text file. </p>
  
  <p>I print that output I want to the screen every time that loop is executed but tried the dozens of answers in this site regarding writing to a text file but all failed. 
  here is snippet of the code that contains the for loop</p>
  
  <pre><code>for t in numpy.arange(0, t_end, Dt):
      print 'Time ', t, 'Max_temp ', "%.3E " % T.vector().array().max()
  
      line_n = int(abs(t / line_time))
      hatch = 0.0002
  
      if (line_n % 2) == 0 
          f.xx = (0.001 + vel*t - (length*line_n - mis))
  
      else:
          f.xx = (0.019 - vel*t + (length*line_n - mis))
          f.yy = 0.001 + line_n * hatch
  
      solve(A, T.vector(), b, 'cg')
      print 'Line#', t,
      timestep += 1
      T0.assign(T)
  </code></pre>
  
  <p>now I want to write the output of the two print statements up there to text file instead of writing that to the screen.</p>
  
  <p>P.S. I use a Linux machine  </p>