FAQ
Hi All,



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.



There is another way to check the write permissions by creating a
temporary file inside the directory & then removing it. Catch the
exception if create file is not allowed to decide the iswritable value.
But this is changing the last modification time (timestamp) of the
directory which is a critical issue for our application.



Please suggest a solution.



Thanks

Ayaskant-

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110520/8610874a/attachment.html>

Search Discussions

  • Tim Golden at May 19, 2011 at 7:43 pm

    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 at May 19, 2011 at 7:56 pm

    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 at May 19, 2011 at 8:08 pm

    On 19/05/2011 20:56, Andrew Berg wrote:
    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.
    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 at May 19, 2011 at 8:40 pm

    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 at May 20, 2011 at 8:21 am

    On 19/05/2011 21:40, Andrew Berg wrote:
    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. :-)
    (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 at May 20, 2011 at 8:53 am
    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
  • Ayaskanta Swain at May 20, 2011 at 11:26 am
    Hi Tim,



    Thanks for the reply and suggestions. I followed the patch provided by
    you in issue 2528, but the code looks very tricky to me. 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



    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/python-list/attachments/20110520/9a687587/attachment.html>
    -------------- next part --------------
    A non-text attachment was scrubbed...
    Name: Test.py
    Type: application/octet-stream
    Size: 1577 bytes
    Desc: Test.py
    URL: <http://mail.python.org/pipermail/python-list/attachments/20110520/9a687587/attachment.obj>
  • Tim Golden at May 24, 2011 at 2:40 pm

    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
    >

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedMay 19, '11 at 7:37p
activeMay 24, '11 at 2:40p
posts9
users3
websitepython.org

People

Translate

site design / logo © 2022 Grokbase