title: Writing a line of row per iteration to file without overwriting the file in python
body: <p>I have searched this on Stackoverflow and all of the "duplicates" for this topic but it seems remained unanswered. I have tried all these:</p>
  
  <p><strong>Attempt#1:</strong></p>
  
  <pre><code>for word in header:
      writer.writerow([word]
  </code></pre>
  
  <p><em>Pasted from <a href="https://stackoverflow.com/questions/14134237/writing-data-from-a-python-list-to-csv-row-wise">writing data from a python list to csv row-wise</a></em> </p>
  
  <p><strong>Attempt#2:</strong></p>
  
  <p>And this one, should have been close but it has a bug:</p>
  
  <pre><code># Open a file in witre mode
  fo = open("foo.txt", "rw+")
  print "Name of the file: ", fo.name
  
  
  Pasted from &lt;http://www.tutorialspoint.com/python/file_writelines.htm&gt; 
  
  # Assuming file has following 5 lines
  # This is 1st line
  # This is 2nd line
  # This is 3rd line
  # This is 4th line
  # This is 5th line
  
  seq = ["This is 6th line\n", "This is 7th line"]
  # Write sequence of lines at the end of the file.
  fo.seek(0, 2)
  line = fo.writelines( seq )
  
  # Now read complete file from beginning.
  fo.seek(0,0)
  for index in range(7):
     line = fo.next()
     print "Line No %d - %s" % (index, line)
  
  # Close opend file
  fo.close()
  </code></pre>
  
  <p><em>Pasted from <a href="http://www.tutorialspoint.com/python/file_writelines.htm" rel="nofollow noreferrer">http://www.tutorialspoint.com/python/file_writelines.htm</a></em> </p>
  
  <p><strong>Attempt#3</strong>:</p>
  
  <pre><code>&gt;&gt;&gt;outF = open("myOutFile.txt", "w")
  &gt;&gt;&gt;for line in textList:
  ...    outF.write(line)
  ...    outF.write("\n")
  &gt;&gt;&gt;outF.close()
  </code></pre>
  
  <p><em>Pasted from <a href="http://cmdlinetips.com/2012/09/three-ways-to-write-text-to-a-file-in-python/" rel="nofollow noreferrer">http://cmdlinetips.com/2012/09/three-ways-to-write-text-to-a-file-in-python/</a></em> </p>
  
  <p><strong>Attempt#4:</strong> </p>
  
  <pre><code>with open('file_to_write', 'w') as f:
      f.write('file contents')
  </code></pre>
  
  <p><em>Pasted from <a href="https://stackoverflow.com/questions/6159900/correct-way-to-write-line-to-file-in-python">Correct way to write line to file in Python</a></em> </p>
  
  <p><strong>Attempt#5:</strong> </p>
  
  <p>This one which uses the append when writing to file.. but it appends each line at the end of each row. So it would be hard for me to separate all the rows. </p>
  
  <pre><code>append_text = str(alldates)
  with open('my_file.txt', 'a') as lead:
      lead.write(append_text)
  </code></pre>
  
  <p><em>Pasted from <a href="https://stackoverflow.com/questions/22371625/python-saving-a-string-to-file-without-overwriting-files-contents">Python: Saving a string to file without overwriting file&#39;s contents</a></em> </p>
  
  <p><em>Can someone help me how to write a newline of row per iteration in a loop to a file without overwriting the file?</em></p>