title: Python: Writing/appending to a file inside a thread class error
body: <p>Im trying to periodcally write some measurements into a txt file. (some sorta logging).</p>
  
  <p>To periodically run a function, there is a RepeatTimer class. And to run the function of updating the file I have a VTlog() function as below.</p>
  
  <pre><code>class RepeatTimer(Thread):
      def __init__(self, interval, function, iterations=0, args=[], kwargs={}):
          Thread.__init__(self)
          self.interval = interval
          self.function = function
          self.iterations = iterations
          self.args = args
          self.kwargs = kwargs
          self.finished = Event()
  
      def run(self):
          count = 0
          while not self.finished.isSet() and (self.iterations &lt;= 0 or count &lt; self.iterations):
              self.finished.wait(self.interval)
              if not self.finished.isSet():
                  self.function(*self.args, **self.kwargs)
                  count += 1
  
      def cancel(self):
          self.finished.set() 
  
  
  
  def LogVT():
      tgt_picks = [t1,t2] #modify here for selective targets
      for tgt in tgt_picks:
          tt = tgt
          file = ('c:/vtlogs_%s.txt' % str(tt.target_name))
          x = str(tt.voltage('v_1p05_gfx_uncore')) + ', ' + str(tt.voltage('v_1p5_sm')) + ', ' + str(tt.temperature('pch_temp'))
          q = time.strftime('%m/%d/%y, %H:%M:%S')
          filehandle = open(file, 'a')
          filehandle.write('\n' + q + ', ' + x)
          filehandle.close()
          time.sleep(3)
  
  
  logtimer = RepeatTimer(60.0, LogVT)
  logtimer.start()
  </code></pre>
  
  <p>the 'x' and 'q' works separetely when queried offline. t1 &amp; t2 are some system attached with voltage &amp; temp measurement device. api is already initiated.</p>
  
  <p>My issue is, Im getting this at runtime:</p>
  
  <pre><code>Traceback (most recent call last):
    File "C:\Python25\lib\logging\__init__.py", line 750, in emit
      self.stream.write(fs % msg)
  ValueError: I/O operation on closed file
  </code></pre>
  
  <p>Any explanation why??</p>