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


Groups > comp.lang.python > #33325

Re: Detect file is locked - windows

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <mail@timgolden.me.uk>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'subject:file': 0.07; 'python': 0.09; 'errno': 0.09; 'ioerror': 0.09; 'module)': 0.09; 'open()': 0.09; 'pywin32': 0.09; 'security.': 0.09; 'throw': 0.09; 'extension': 0.13; 'cases': 0.15; 'file,': 0.15; "'w'": 0.16; '(via': 0.16; "file's": 0.16; 'from:addr:timgolden.me.uk': 0.16; 'from:name:tim golden': 0.16; 'message-id:@timgolden.me.uk': 0.16; 'posix': 0.16; 'received:74.55.86': 0.16; 'received:74.55.86.74': 0.16; 'received:smtp.webfaction.com': 0.16; 'received:webfaction.com': 0.16; 'subject:windows': 0.16; 'tjg': 0.16; 'later': 0.16; 'wrote:': 0.17; 'module': 0.19; 'changes': 0.20; 'permission': 0.20; 'file.': 0.20; 'trying': 0.21; 'regardless': 0.21; 'ctypes': 0.22; 'errors': 0.23; 'header:In- Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'skip:( 20': 0.28; 'mode': 0.30; 'file': 0.32; 'not.': 0.32; 'could': 0.32; 'received:192.168.100': 0.33; 'handle': 0.33; 'to:addr:python- list': 0.33; 'another': 0.33; 'changed': 0.34; 'open': 0.35; 'really': 0.36; 'modules': 0.36; 'does': 0.37; 'two': 0.37; 'far': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'application': 0.40; 'received:192.168': 0.40; "you've": 0.61; "you'll": 0.62; 'situation': 0.62; '13.': 0.65; 'from:addr:mail': 0.71; 'beforehand': 0.84; 'distinguish': 0.84; 'situations,': 0.84; 'ali': 0.95
Date Wed, 14 Nov 2012 11:50:49 +0000
From Tim Golden <mail@timgolden.me.uk>
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20120907 Thunderbird/15.0.1
MIME-Version 1.0
To python-list@python.org
Subject Re: Detect file is locked - windows
References <2af436e3-1336-42ae-aa2f-9e33135a3f2c@googlegroups.com>
In-Reply-To <2af436e3-1336-42ae-aa2f-9e33135a3f2c@googlegroups.com>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3678.1352893859.27098.python-list@python.org> (permalink)
Lines 28
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1352893859 news.xs4all.nl 6853 [2001:888:2000:d::a6]:58129
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:33325

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

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

csiph-web