This fixes "The process cannot access the file because it is being used by another process" errors on Windows. The file is still automatically deleted by the `os.unlink` call later in the function. Updates #5606 Fixes #5642