How to find a file or a device is currently used by which processor which program ?

Tim Golden tim.golden at viacom-outdoor.co.uk
Wed Oct 11 11:27:12 CEST 2006


[fdu.xiaojf at gmail.com]

| When I want to uninstall my usb disk on windows, the 
| operating systems 
| sometimes tells me the device is being used by other program. But I 
| can't find which program is using it. Can I do this using python ?

This is really one of those questions that could better
be rephrased: can I do this *without* using Python? By
which I mean: there's nothing built into Python or its
standard libraries which will determine which open program
is holding which files open on your filesystem. I'm quite
certain there exists some more or less brute-force 
technique for this in Windows in general, but you'd really
have to find that, and then determine how to do it in
Python.

(Bit of Googling later...)
I suggest you look at the Handle util from sysinternals.com
(always a good place to start looking for these kind of
things):

http://www.sysinternals.com/utilities/handle.html

You can run it against a directory or a specific file
and its output it fairly parseable. Example, where I
have a python process holding open a "temp.tmp" file
my c:\temp directory:

<dump>
C:\>handle c:\temp

Handle v3.2
Copyright (C) 1997-2006 Mark Russinovich
Sysinternals - www.sysinternals.com

cmd.exe            pid: 4260      C: C:\temp
python.exe         pid: 5296     58: C:\temp\temp.tmp
cmd.exe            pid: 5232     94: C:\temp
python.exe         pid: 2672     94: C:\temp

C:\>
</dump>

You could use the subprocess module (or its predecessors) to
scan the output fairly easily, I imagine. What you with the 
process name / id is up to your needs, of course :)

TJG

________________________________________________________________________
This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________



More information about the Python-list mailing list