Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Detect file is locked - windows

34 views
Skip to first unread message

Ali Akhavan

unread,
Nov 13, 2012, 7:33:17 PM11/13/12
to
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.

Thanks,
nomadali

Mark Lawrence

unread,
Nov 13, 2012, 8:14:59 PM11/13/12
to pytho...@python.org
Anything here help http://www.python.org/dev/peps/pep-3151/ ?

--
Cheers.

Mark Lawrence.

Hans Mulder

unread,
Nov 14, 2012, 3:55:15 AM11/14/12
to
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"
That won't help: in Python 3.3, IOError with errno==13 has been
replaced by PermissionError. It still doesn't tell you *why*
you got a PermissionError.


Hope this helps,

-- HansM

Tim Golden

unread,
Nov 14, 2012, 4:06:11 AM11/14/12
to Hans Mulder, pytho...@python.org
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.

TJG

Tim Golden

unread,
Nov 14, 2012, 5:02:45 AM11/14/12
to pytho...@python.org
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.

What version of Python are you using?

>
> How can I distinguish these two cases ? Namely, if some application
> has the file open or not.

Can I ask what you expect to do differently in each of those cases? In
other words, if you can't access the file, you can't access it. (Not to
dismiss your question; I just wonder how you're going to handle the
different cases)

TJG

Tim Golden

unread,
Nov 14, 2012, 6:50:49 AM11/14/12
to pytho...@python.org
On 14/11/2012 00:33, Ali Akhavan wrote:
The Python io module calls into the MS CRT, which maps both errors
(ERROR_ACCESS_DENIED & ERROR_SHARING_VIOLATION) to posix errno EACCESS,
which is 13.


If you really need to distinguish the two situations, you'll need to
call CreateFile directly (via ctypes or the pywin32 modules or an
extension module) and then call GetLastError() to get the specific
condition.

You're far better off using this EAFP approach as, even if it were
simple to determine beforehand whether a file can be locked or read --
and it's not -- that situation could have changed by the time you
actually come to open it.

Once you've successfully got a handle to the file, that handle is valid
regardless of any later changes to the file's security.

TJG

Hans Mulder

unread,
Nov 14, 2012, 6:51:01 AM11/14/12
to
It would be nice if he could give specific error messages, e.g.

"Can't write %s because it is locked by %s."

vs.

"Can't write %s because you don't have write access."

I can't speak for Ali, but I'm always annoyed by error messages
listing several possible cuases, such as "Can't delete file,
because the source or destination is in use".

-- HansM

Tim Golden

unread,
Nov 14, 2012, 7:09:07 AM11/14/12
to pytho...@python.org
On 14/11/2012 11:51, Hans Mulder wrote:
> It would be nice if he could give specific error messages, e.g.
>
> "Can't write %s because it is locked by %s."
>
> vs.
>
> "Can't write %s because you don't have write access."
>
> I can't speak for Ali, but I'm always annoyed by error messages
> listing several possible cuases, such as "Can't delete file,
> because the source or destination is in use".

(I realise you're not demanding this particular behaviour from Python
but just to expand on what the obstacles are to this at present):

Speaking merely from the point of view of the current Python
implementation on Windows, there are two obstacles to this:

* Python calls into the CRT which simply returns 13 (EACCESS) for both
of these situations. Obviously, Python could do its own thing on
Windows, partly reimplementing what the CRT does anyway and giving more
precise feedback. Equally obviously, this wouldn't be a trivial exercise.

* The added information -- who's locked the file, what permissions are
in place which prevent you gaining the requested access -- is
surprisingly fiddly to get hold of and would be something of an overhead
for the majority of the time when it's not wanted. Of course, in this
hypothetical Python one could add some sort of flag to the open()
function which requested or not the additional information.

The first obstacle is more significant than the second but neither is
negligible.

TJG

Aahz

unread,
Nov 15, 2012, 11:01:45 AM11/15/12
to
In article <mailman.3676.1352887...@python.org>,
Tim Golden <ma...@timgolden.me.uk> 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.
>
>Can I ask what you expect to do differently in each of those cases? In
>other words, if you can't access the file, you can't access it. (Not to
>dismiss your question; I just wonder how you're going to handle the
>different cases)

Real-life use case for user-requested operation: if no permission, skip
the file "permanently" (until it changes, at least); if locked, place in
retry loop.

And in response to your other post, you absolutely want to use
CreateFileW()... ;-)
--
Aahz (aa...@pythoncraft.com) <*> http://www.pythoncraft.com/

"LL YR VWL R BLNG T S" -- www.nancybuttons.com
0 new messages