Detect file is locked - windows

A

Ali Akhavan

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
 
M

Mark Lawrence

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/ ?
 
H

Hans Mulder

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
 
T

Tim Golden

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
 
T

Tim Golden

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
 
T

Tim Golden

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
 
H

Hans Mulder

What version of Python are you using?


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
 
T

Tim Golden

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
 
A

Aahz

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()... ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top