Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Detect file is locked - windows

Reply
Thread Tools

Detect file is locked - windows

 
 
Ali Akhavan
Guest
Posts: n/a
 
      11-14-2012
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
 
Reply With Quote
 
 
 
 
Mark Lawrence
Guest
Posts: n/a
 
      11-14-2012
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.

 
Reply With Quote
 
 
 
 
Hans Mulder
Guest
Posts: n/a
 
      11-14-2012
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
 
Reply With Quote
 
Tim Golden
Guest
Posts: n/a
 
      11-14-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.

TJG
 
Reply With Quote
 
Tim Golden
Guest
Posts: n/a
 
      11-14-2012
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
 
Reply With Quote
 
Tim Golden
Guest
Posts: n/a
 
      11-14-2012
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
 
Reply With Quote
 
Hans Mulder
Guest
Posts: n/a
 
      11-14-2012
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
 
Reply With Quote
 
Tim Golden
Guest
Posts: n/a
 
      11-14-2012
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
 
Reply With Quote
 
Aahz
Guest
Posts: n/a
 
      11-15-2012
In article <(E-Mail Removed)>,
Tim Golden <(E-Mail Removed)> 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 ((E-Mail Removed)) <*> http://www.pythoncraft.com/

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

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How to unlock a locked file under Windows? red floyd C++ 0 08-25-2009 04:07 PM
Re: How to unlock a locked file under Windows? Victor Bazarov C++ 1 08-25-2009 02:02 PM
Determining if a file is locked in Windows elake Python 6 10-20-2006 04:08 PM
A failure occurred writing to the resources file. Access is denied. -- RESX file is locked? -- WHY? Mark Kamoski ASP .Net 1 07-04-2003 12:02 PM



Advertisments