title: Python: Creating an empty file object
body: <p>I am attempting to make a logging module for Python that does not work because it fails on creation of the file object.</p>
  
  <p>debug.py:</p>
  
  <pre><code>import os
  import datetime
  import globals
  
  global fil
  fil = None
  
  def init(fname):
       fil = open(fname, 'w+')
       fil.write("# PyIDE Log for" + str(datetime.datetime.now()))
  
  def log(strn):
      currentTime = datetime.datetime.now()
  
      fil.write(str(currentTime) + ' ' + str(os.getpid()) + ' ' + strn)
      print str(currentTime) + ' ' + str(os.getpid()) + ' ' + strn
  
  def halt():
      fil.close()
  </code></pre>
  
  <p>fil will not work as <code>None</code> as I get an <code>AttributeError</code>. I also tried creating a dummy object:</p>
  
  <pre><code>fil = open("dummy.tmp","w+")
  </code></pre>
  
  <p>but the <code>dummy.tmp</code> file is written to instead, even though <code>init()</code> is called before <code>log()</code> is. Obviously you cannot open a new file over an already opened file. I attempted to close <code>fil</code> before <code>init()</code>, but Python said it could not perform <code>write()</code> on a closed file.</p>
  
  <p>This is the code that is accessing <code>debug.py</code></p>
  
  <pre><code>if os.path.exists(temp):
           os.rename(temp, os.path.join("logs","archived","log-" + str(os.path.getctime(temp)) + ".txt"))
           debug.init(globals.logPath)
           debug.log("Logger initialized!")
  </code></pre>
  
  <p>I would like to have logging in my program and I cannot find a workaround for this.</p>