[Python] PermissionError when using python 3.3.4 and RotatingFileHandler

Discussão em 'Python' iniciado por Stack, Agosto 26, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to get a rotating log file for a GUI application I am writing with python 3.3.4 and PyQt4.

    I have the following snippet of code in my main script:

    import logging
    import resources

    logger = logging.getLogger('main.test')

    def main():
    logger.setLevel(logging.DEBUG)

    fh = RotatingFileHandler(resources.LOG_FILE_PATH, maxBytes=500, backupCount=5)
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    fh.setFormatter(formatter)

    logger.addHandler(fh)
    logger.info('main')


    I have the maxBytes low so that I can test the rotating is working correctly, which it is not. I am getting the following error whenever the log should be rotated:

    Traceback (most recent call last):
    File "C:\Python33\lib\logging\handlers.py", line 73, in emit
    self.doRollover()
    File "C:\Python33\lib\logging\handlers.py", line 176, in doRollover
    self.rotate(self.baseFilename, dfn)
    File "C:\Python33\lib\logging\handlers.py", line 116, in rotate
    os.rename(source, dest)
    PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\myuser\\.logtest\\test.log.1'


    And nothing is logged. Any help is much appreciated. Thank you

    Continue reading...

Compartilhe esta Página