Detect file is locked - windows

Discussion in 'Python' started by Ali Akhavan, Nov 14, 2012.

  1. Ali Akhavan

    Ali Akhavan Guest

    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
    Ali Akhavan, Nov 14, 2012
    #1
    1. Advertising

  2. 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.
    Mark Lawrence, Nov 14, 2012
    #2
    1. Advertising

  3. Ali Akhavan

    Hans Mulder Guest

    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
    Hans Mulder, Nov 14, 2012
    #3
  4. Ali Akhavan

    Tim Golden Guest

    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, Nov 14, 2012
    #4
  5. Ali Akhavan

    Tim Golden Guest

    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, Nov 14, 2012
    #5
  6. Ali Akhavan

    Tim Golden Guest

    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
    Tim Golden, Nov 14, 2012
    #6
  7. Ali Akhavan

    Hans Mulder Guest

    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
    Hans Mulder, Nov 14, 2012
    #7
  8. Ali Akhavan

    Tim Golden Guest

    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
    Tim Golden, Nov 14, 2012
    #8
  9. Ali Akhavan

    Aahz Guest

    In article <>,
    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.
    >>
    >> 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 () <*> http://www.pythoncraft.com/

    "LL YR VWL R BLNG T S" -- www.nancybuttons.com
    Aahz, Nov 15, 2012
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Kamoski
    Replies:
    1
    Views:
    2,440
  2. elake
    Replies:
    6
    Views:
    325
    Ricardo Reyes
    Oct 20, 2006
  3. Victor Bazarov
    Replies:
    1
    Views:
    470
    Vladimir Jovic
    Aug 25, 2009
  4. red floyd
    Replies:
    0
    Views:
    741
    red floyd
    Aug 25, 2009
  5. Chris Withers

    python install on locked down windows box?

    Chris Withers, Sep 22, 2011, in forum: Python
    Replies:
    4
    Views:
    1,896
    Chris Withers
    Sep 23, 2011
Loading...

Share This Page