Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #33276 > unrolled thread

Detect file is locked - windows

Started byAli Akhavan <a.akhavan.b@gmail.com>
First post2012-11-13 16:33 -0800
Last post2012-11-14 11:50 +0000
Articles 9 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  Detect file is locked - windows Ali Akhavan <a.akhavan.b@gmail.com> - 2012-11-13 16:33 -0800
    Re: Detect file is locked - windows Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-11-14 01:14 +0000
      Re: Detect file is locked - windows Hans Mulder <hansmu@xs4all.nl> - 2012-11-14 09:55 +0100
        Re: Detect file is locked - windows Tim Golden <mail@timgolden.me.uk> - 2012-11-14 09:06 +0000
    Re: Detect file is locked - windows Tim Golden <mail@timgolden.me.uk> - 2012-11-14 10:02 +0000
      Re: Detect file is locked - windows Hans Mulder <hansmu@xs4all.nl> - 2012-11-14 12:51 +0100
        Re: Detect file is locked - windows Tim Golden <mail@timgolden.me.uk> - 2012-11-14 12:09 +0000
      Re: Detect file is locked - windows aahz@pythoncraft.com (Aahz) - 2012-11-15 08:01 -0800
    Re: Detect file is locked - windows Tim Golden <mail@timgolden.me.uk> - 2012-11-14 11:50 +0000

#33276 — Detect file is locked - windows

FromAli Akhavan <a.akhavan.b@gmail.com>
Date2012-11-13 16:33 -0800
SubjectDetect file is locked - windows
Message-ID<2af436e3-1336-42ae-aa2f-9e33135a3f2c@googlegroups.com>
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

[toc] | [next] | [standalone]


#33283

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2012-11-14 01:14 +0000
Message-ID<mailman.3659.1352855611.27098.python-list@python.org>
In reply to#33276
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.
>
> Thanks,
> nomadali
>

Anything here help http://www.python.org/dev/peps/pep-3151/ ?

-- 
Cheers.

Mark Lawrence.

[toc] | [prev] | [next] | [standalone]


#33314

FromHans Mulder <hansmu@xs4all.nl>
Date2012-11-14 09:55 +0100
Message-ID<50a35c73$0$6970$e4fe514c@news2.news.xs4all.nl>
In reply to#33283
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"

> Anything here help http://www.python.org/dev/peps/pep-3151/ ?

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

[toc] | [prev] | [next] | [standalone]


#33317

FromTim Golden <mail@timgolden.me.uk>
Date2012-11-14 09:06 +0000
Message-ID<mailman.3674.1352883975.27098.python-list@python.org>
In reply to#33314
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

[toc] | [prev] | [next] | [standalone]


#33320

FromTim Golden <mail@timgolden.me.uk>
Date2012-11-14 10:02 +0000
Message-ID<mailman.3676.1352887368.27098.python-list@python.org>
In reply to#33276
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

[toc] | [prev] | [next] | [standalone]


#33326

FromHans Mulder <hansmu@xs4all.nl>
Date2012-11-14 12:51 +0100
Message-ID<50a385a5$0$6916$e4fe514c@news2.news.xs4all.nl>
In reply to#33320
On 14/11/12 11:02:45, Tim Golden 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.
> 
> 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)

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

[toc] | [prev] | [next] | [standalone]


#33328

FromTim Golden <mail@timgolden.me.uk>
Date2012-11-14 12:09 +0000
Message-ID<mailman.3680.1352894950.27098.python-list@python.org>
In reply to#33326
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

[toc] | [prev] | [next] | [standalone]


#33392

Fromaahz@pythoncraft.com (Aahz)
Date2012-11-15 08:01 -0800
Message-ID<k833l9$8e4$1@panix5.panix.com>
In reply to#33320
In article <mailman.3676.1352887368.27098.python-list@python.org>,
Tim Golden  <mail@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 (aahz@pythoncraft.com)           <*>         http://www.pythoncraft.com/

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

[toc] | [prev] | [next] | [standalone]


#33325

FromTim Golden <mail@timgolden.me.uk>
Date2012-11-14 11:50 +0000
Message-ID<mailman.3678.1352893859.27098.python-list@python.org>
In reply to#33276
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.

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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web