I have some code which runs fine on unix systems, but not on Windows. I'd like to make it cross-platform, but am banging my head against the wall. The minimal repro is as follows:
File 1: foo.py
import os
import sys
import logging
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
logger = logging.getLogger('foo')
def main(dir):
logger.addHandler(logging.FileHandler(
os.path.join(dir, 'temporary.log')
))
logger.info("Hello, world!")
File 2: main.py
from foo import main
import tempfile
if __name__ == "__main__":
with tempfile.TemporaryDirectory("test") as tmp:
main(tmp)
What I'd expect is that the temporary directory would be created, a file would be created within that to which logs would be emitted, and then both would be cleaned up when tmp
goes out of scope.
Instead, Windows provides an error:
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: '...'
I've tried changing the mode of the FileHandler
away from append mode, I've tried manually cleaning up the files and directories, I've tried delaying the file from being created until its logged to and cranked up the log level, and I've even tried instantiating the logger inside foo.main
in hopes that no references to the handler would be persist -- regardless, I still see this error.
How can I fix this?
main
function could keep a reference to the handler to calllogger.removeHandler(handler)
in afinally
block. This would ensure that the file is closed beforemain
returns. – Colenecoleopteran