title: Python logger `RotatingFileHandler` failing under Windows
body: <p>I am trying to use the <code>RotatingFileHandler</code> handler to manage potentially long logfiles for a Python project running under Windows. I have found that the log file rotation works fine when run using the IDLE interpreter (on a Windows PC) but fails with a WindowsError exception when run from the Windows command prompt.</p>
  
  <p>Here is an example code that shows the problem - </p>
  
  <pre><code>import logging, logging.handlers
  import datetime
  import time
  
  mainlogfile = 'fred.log'
  logging_level = logging.DEBUG
  logging_rotate_time = datetime.timedelta(minutes=1)
  
  logger = logging.getLogger('Main_Logger')
  logger.setLevel(logging.DEBUG)
  
  handler = logging.handlers.RotatingFileHandler(mainlogfile, backupCount=7)
  handler.setLevel(logging_level)
  formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
  handler.setFormatter(formatter)      
  logger.addHandler(handler)
  
  next_rotate = datetime.datetime.now() + logging_rotate_time
  
  while True:
      time.sleep(5)
      logger.info('Tick ')
      n = datetime.datetime.now()
      if n&gt;next_rotate:
          logger.info('Rotating logfile')
          handler.doRollover()
          logger.info('Succesfully rotated logfile')
          next_rotate = next_rotate + logging_rotate_time
          logger.info('Next logfile rotate at '+ str(next_rotate))
  </code></pre>
  
  <p>When I run this in the IDLE interpreter, it works fine and the log file <code>fred.log</code> is rotated every minute producing <code>fred.log.1, fred.log.2</code> etc. with contents such as -</p>
  
  <pre><code>2018-12-10 12:24:40,269 INFO Succesfully rotated logfile
  2018-12-10 12:24:40,269 INFO Next logfile rotate at 2018-12-10 12:25:40.182000
  2018-12-10 12:24:45,269 INFO Tick 
  2018-12-10 12:24:50,269 INFO Tick 
  2018-12-10 12:24:55,269 INFO Tick 
  2018-12-10 12:25:00,267 INFO Tick 
  2018-12-10 12:25:05,267 INFO Tick 
  2018-12-10 12:25:10,266 INFO Tick 
  2018-12-10 12:25:15,266 INFO Tick 
  2018-12-10 12:25:20,266 INFO Tick 
  2018-12-10 12:25:25,266 INFO Tick 
  2018-12-10 12:25:30,265 INFO Tick 
  2018-12-10 12:25:35,265 INFO Tick 
  2018-12-10 12:25:40,263 INFO Tick 
  2018-12-10 12:25:40,263 INFO Rotating logfile
  </code></pre>
  
  <p>However, when run in a command prompt it fails on the first rotate attempt -</p>
  
  <pre><code>C:\&gt; python try10.py
  Traceback (most recent call last):
    File "try10.py", line 29, in &lt;module&gt;
      handler.doRollover()
    File "C:\Python27\lib\logging\handlers.py", line 142, in doRollover
      os.rename(self.baseFilename, dfn)
  WindowsError: [Error 32] The process cannot access the file because it is being used by another process
  </code></pre>
  
  <p>I have searched and found that there <em>are</em> problems under Windows when trying to close or rename log files that are in use by another Python process but I can't see how that is relevant to my simple example. I have tried calling <code>handler.flush()</code> and <code>handler.close()</code> before <code>handler.doRollover()</code> but this did not change the behavior. </p>
  
  <p>My questions are -</p>
  
  <ul>
  <li>Why is the exception raised when run at the command prompt but not under IDLE?</li>
  <li>Is there any changes I can make to the code to allow it to run at the Windows command prompt</li>
  </ul>