title: Write line with timestamp+message to file
body: <p>I want to create a logfile that adds every time an error occurs a new line to a textfile log.txt. I am pretty new to python, so maybe I miss something...but everytime an error occurs, the log.txt is overwritten and only the current error message is displayed although the error message is different every time (due to timestamp) and I added a \n.</p>
  
  <p>Thats my code so far:</p>
  
  <p><pre>
  import os
  import sys
  import time
  import datetime</p>
  
  <p>try:
      path = sys.argv[1]</p>
  
  <code>ts = time.time()
  sttime = datetime.datetime.fromtimestamp(ts).strftime('%Y%m%d_%H:%M:%S - ')
  
  #some more things but nothing else of interest for here
  
  except:
      error = "ERROR! No file 'bla' found!"
      log = 'log.txt'
      logfile = file(log, "w")
      logfile.write(sttime + error + '\n')
      logfile.close()
      sys.exit(0)
  </code></pre>
  
  <p>Maybe you can help me out here. Do I need a loop somewhere? I tried to create an empty string (error = "") that adds the error message to log.txt with += each time an error occurs, but that didn't work at all :-/</p>
  
  <p>Thank you!</p>