If you want to unconditionally force the closure of the active handle so you can delete the file, you could leverage Microsoft's handle tool using the file name as the argument (which will return all file handles with that string in the object name), and then the invoke handle again using the -c
option specifying the exact handle to close and the pid it belongs to.
I have used this method successfully in the past in cases where I knew I wanted to absolutely, unconditionally kill the active handles on a particular file/directory so I could proceed with another action.
However, keep in mind that, as the documentation for handle states:
WARNING: Closing handles can cause application or system instability.
You can use subprocess.check_output
to invoke handle.
More subprocess info