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
In any case, this information should really be reflected in the docs.
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
(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
[... 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
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
>