(Winows) Finding out which process has locked a file.

Dave Angel davea at ieee.org
Wed May 13 12:23:15 EDT 2009

CinnamonDonkey wrote:
> Hi all,
> Does anyone know how I can programatically find out which process
> (resolved to human friendly string, i.e. executable) has a lock on a
> file.
> I have a script running which occassionally fails because it is trying
> to delete a file in use by another process. When this happens I want
> it to log which process has the lock.
> Since it tends to happen when I am never near the computer to catch
> it! :(
> Cheers,
> Shaun
This may depend on what version of Windows you're using, as the API's 
changed in Windows 2000, and again in XP SP2, and probably again in Vista.

I don't know an API for what you want, but filemon, from 
can build a log of file activity, which you can examine later.

If you do find an API that works on XP and better, please let us know.  
Even if you only know a function name, I'd be willing to play with it 
some, to see what it might do.  I'd like to find out, and would share 
what I did figure out.

More information about the Python-list mailing list