Detect file is locked - windows

Tim Golden mail at
Wed Nov 14 10:06:11 CET 2012

On 14/11/2012 08:55, Hans Mulder wrote:
> On 14/11/12 02:14:59, Mark Lawrence wrote:
>> On 14/11/2012 00:33, Ali Akhavan wrote:
>>> I am trying to open a file in 'w' mode open('file', 'wb'). open() will
>>> throw with IOError with errno 13 if the file is locked by another
>>> application or if user does not have permission to open/write to the
>>> file.
>>> How can I distinguish these two cases ? Namely, if some application
>>> has the file open or not.
> I don't have a Windows machine at hand to try, but this might work:
>     if exc.errno == 13:
>         if os.access('file', os.W_OK):
>             print "Locked by another process"
>         else:
>             print "No permission to write"

No luck, I'm afraid. os.access on Windows is basically non-functional
(and would have been deprecated if I'd actually got around to doing it).
It basically checks the old-style readonly flag and that's it. IOW,
you'd return True for a file whose attributes you could read regardless
of whether you could read/write the file contents.


More information about the Python-list mailing list