Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: os.access giving incorrect results on Windows

20 views
Skip to first unread message

Tim Golden

unread,
May 19, 2011, 3:43:55 PM5/19/11
to pytho...@python.org
On 19/05/2011 20:37, Ayaskanta Swain wrote:
> Please help me in solving this issue. I want to check the write
> permissions on a directory on windows from my python script.
>
> I tried to use *os.access(dirpath, os.W_OK)*to check whether the user
> has write access or not, but it gives me incorrect result. It always
> gives me False even if the user has write permission. Interestingly this
> function works just fine on Linux platforms.

This is basically issue2528 [1].
The problem is that, although Windows (and Python)
expose a version of os.access to match the Posix function,
the meaning is so far removed on Windows as to be useless.

Really what you need to do is use the AccessCheck API, which
is a little bit tortuous, but is intended for this purpose.
You can look at the code in my patch for that issue to get
an idea of how to do it.

Does that help?

TJG


[1] http://bugs.python.org/issue2528

Andrew Berg

unread,
May 19, 2011, 3:56:17 PM5/19/11
to pytho...@python.org
On 2011.05.19 02:43 PM, Tim Golden wrote:
> This is basically issue2528 [1].
> The problem is that, although Windows (and Python)
> expose a version of os.access to match the Posix function,
> the meaning is so far removed on Windows as to be useless.
Does this affect just os.W_OK and directories or all of os.access()?

In any case, this information should really be reflected in the docs.

Tim Golden

unread,
May 19, 2011, 4:08:15 PM5/19/11
to pytho...@python.org

The current code is very naive:

* A R_OK check always succeeds if the file's attributes can be read
at all
* A W_OK check fails if the file has its DOS read-only attribute set
* A W_OK check always succeeds for a directory (because read-only means
something else for directories).

Would you care to propose some wording for the docs? I'm quite happy
to commit if we can come to an agreement.

TJG

Andrew Berg

unread,
May 19, 2011, 4:40:26 PM5/19/11
to pytho...@python.org
On 2011.05.19 03:08 PM, Tim Golden wrote:
> * A R_OK check always succeeds if the file's attributes can be read
> at all
So is this the same as F_OK then, or does it return false if the user
isn't allowed to read permissions?

> * A W_OK check fails if the file has its DOS read-only attribute set
DOS attribute?

> * A W_OK check always succeeds for a directory (because read-only means
> something else for directories).
>
> Would you care to propose some wording for the docs? I'm quite happy
> to commit if we can come to an agreement.
I'm a beginner when it comes to Python, but I could give it a shot. A
big red warning box explaining how the code under Windows doesn't use
ACLs under the os.access() entry (above the notes) seems appropriate. A
warning box under os.W_OK saying something like "Under Windows, access()
will always indicate that a directory is writable." would also fit. You
know more about this than I do. I'm running Windows 7 right now and I
have a Python 3.2 interpreter window open if you want me to test/confirm
something. :-)

Tim Golden

unread,
May 20, 2011, 4:21:54 AM5/20/11
to pytho...@python.org

(Sorry; just got back to this this morning). I might raise this on
python-dev. It seems to me that the docs for os.access:

http://docs.python.org/library/os.html#os.access

are already fairly involved. And we try not to make the official
docs too confusing. That said, there's clearly an issue here,
albeit one which doesn't raise its head too often.

There's a case for re-assessing the patch I proposed in issue2528
to see whether to apply it appropriately (reworked).

TJG

Tim Golden

unread,
May 20, 2011, 4:53:53 AM5/20/11
to pytho...@python.org
On 20/05/2011 09:21, Tim Golden wrote:

[... re os.access on Windows ...]

> (Sorry; just got back to this this morning). I might raise this on
> python-dev.

If you want to follow, my post is here:

http://mail.python.org/pipermail/python-dev/2011-May/111530.html

TJG

Tim Golden

unread,
May 24, 2011, 10:40:53 AM5/24/11
to pytho...@python.org
On 20/05/2011 12:26, Ayaskanta Swain wrote:
> Thanks for the reply and suggestions. I followed the patch provided by
> you in issue 2528, but the code looks very tricky to me.

OK, first a summary of the discussion on the python-dev thread.
Essentially it was felt that os.access was sufficiently shaky
and unuseful on Windows that it was better to deprecate it and
to discourage its use. So I'll be making that change when I
can get round to it.

As to your particular problem here...

Does my_dir already exist? If it does then os.open won't be able
to create it. If it doesn't then I can't see any reason why the
code should fail there. I just ran it myself and it fails, as
expected on an unpatched Python, on the assert on line 17 where
the check is made for the result of os.access for W_OK.

I don't have the time right now but if no-one else gets there
first I hope to be able to post back with a standalone example
of the AccessCheck API

TJG

Anyways I wrote
> my Test.py script & tried only the def test_access_w(self): test case
> which is defined under class FileTests(unittest.TestCase) by providing
> my own directory path to check the write permissions on it.
>
> I executed my But it failed with the following errors –
>
> *> python Test.py C:\temp\my_dir*
>
> test_access_w (__main__.FileTests) ... ERROR
>
> ======================================================================
>
> ERROR: test_access_w (__main__.FileTests)
>
> ----------------------------------------------------------------------
>
> Traceback (most recent call last):
>
> File "Test.py", line 14, in test_access_w
>
> f = os.open(dirpath, os.O_CREAT)
>
> OSError: [Errno 13] Permission denied: 'C:\\temp\\my_dir'
>
> ----------------------------------------------------------------------
>
> Ran 1 test in 0.000s
>
> FAILED (errors=1)
>
> Basically the os.open() function is failing to open a directory (In this
> case my_dir). The directory has write permissions for the user. Attached
> herewith is my Test script. Can you please suggest some simple python
> code which checks the write permissions of a directory in a straight
> forward way (Not by using unit tests)
>
> Thanks
>
> Ayaskant-
>
> Bangalore
>

0 new messages