RE: os.access(file, os.R_OK) on UNIX and WINDOWS

Discussion in 'Python' started by Tim Golden, Sep 13, 2006.

  1. Tim Golden

    Tim Golden Guest

    [kai rosenthal]

    | on UNIX I changed the permission of a file "myfile" with chmod 000
    | myfile. Then I got 0 from os.access(myfile, os.R_OK). This is ok.
    |
    | Then I checked the same file on WINDOWS (with samba):
    | I got "True" from os.access(myfile, os.R_OK). I think it is not ok?!

    Ummm. This is a touch similar to a parallel thread about
    os.X_OK on Win32. At the risk of being grilled by better-informed
    types, the fact is that while the Win32 CRT (used for non-Unicode
    operations by the Python os module) does expose chmod and
    access functions, they don't seem guaranteed to do more than overlap
    more-or-less with the corresponding Unix functions.

    From what appears to be the MSDN page for chmod [1]:
    """
    The permission setting controls read and write access to the file.
    The integer expression pmode contains one or both of the following
    manifest constants, defined in SYS\STAT.H:

    _S_IWRITE - Writing permitted
    _S_IREAD - Reading permitted
    _S_IREAD | _S_IWRITE - Reading and writing permitted

    Any other values for pmode are ignored.
    """

    Note that last sentence: I take this to mean that passing
    zero to mean "can't read or write" is having no effect..

    The latest Python source source [2] uses the Win32 API
    SetFileAttributesW [3] where a Unicode filename is passed,
    but this doesn't seem to allow for a neither-read-nor-write
    situation either.

    | In my python script I check the return value of os..access(myfile,
    | os.R_OK) and when it is "True" I copy the file with
    | shutil.copy(myfile, newfile).
    | But on WINDOWS I get the error: IOError: [Errno 13] Permission denied.

    I suspect you're going to have to put the copy in a
    try... except block, which is not bad advice in any
    case. If you check access and then copy, you're at
    the mercy of a change to the file's permissions (or
    even existence) between the calls, so you'd have to
    cope with a potential exception in any case. Better
    perhaps to do it that way in the first place. This
    point of view is supported by, among others, GvR
    in a thread on python-dev. [4]

    [1]
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore9
    8/html/_crt__chmod.2c_._wchmod.asp[2]
    http://svn.python.org/view/python/trunk/Modules/posixmodule.c?rev=51762&
    view=auto
    [3]
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/
    fs/setfileattributes.asp
    [4]
    http://mail.python.org/pipermail/python-dev/2004-November/049931.html

    In short, do something like this:

    <code>
    try:
    shutil.copy (myfile, newfile)
    except IOError:
    print "Couldn't copy; do useful things or ignore"
    else:
    print "Copied ok"
    </code>

    TJG

    ________________________________________________________________________
    This e-mail has been scanned for all viruses by Star. The
    service is powered by MessageLabs. For more information on a proactive
    anti-virus service working around the clock, around the globe, visit:
    http://www.star.net.uk
    ________________________________________________________________________
     
    Tim Golden, Sep 13, 2006
    #1
    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. Replies:
    12
    Views:
    1,653
    Dave Thompson
    Jan 10, 2005
  2. kai rosenthal
    Replies:
    0
    Views:
    461
    kai rosenthal
    Sep 13, 2006
  3. Replies:
    18
    Views:
    626
    Dave Thompson
    Jan 10, 2005
  4. Replies:
    2
    Views:
    351
    Noah Roberts
    Nov 23, 2006
  5. Robert Wallace

    my own perl "dos->unix"/"unix->dos"

    Robert Wallace, Jan 21, 2004, in forum: Perl Misc
    Replies:
    7
    Views:
    281
    Michele Dondi
    Jan 22, 2004
Loading...

Share This Page