Check file is locked?

Tim Golden mail at timgolden.me.uk
Wed Jul 8 09:45:58 CEST 2009


Rajat wrote:
> On Jul 8, 4:57 am, Lawrence D'Oliveiro <l... at geek-
> central.gen.new_zealand> wrote:
>> In message <mailman.2795.1246997268.8015.python-l... at python.org>, Christian
>>
>> Heimes wrote:
>>> By the way most operating systems don't lock a file when it's opened for
>>> reading or writing or even executed.
>> The general conclusion seems to be that mandatory locking is more trouble
>> than it's worth.
> 
> My OS is a windows XP sp3. All I'm trying to achieve is to close an
> application ( which could be a notepad, a wordpad or some other text
> editor) that have opened my file. Once the file is closed I can easily
> delete that file.
> 
> I guess, if file is opened by one of that application, the file must
> be locked and so is the reason I cannot delete the file.


I assume that your real requirement is: I can't open/close/delete
this file; it must be locked by something else; what is that
something else?

The simplest thing is probably to run sysinternals' handle util:

  http://technet.microsoft.com/en-us/sysinternals/bb896655.aspx

and use the subprocess module to parse the output.
That will give you the pid, and you can then use, eg, psutil:

  http://code.google.com/p/psutil/

to get the details of the process.

TJG



More information about the Python-list mailing list