Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > RE: os.access(file, os.R_OK) on UNIX and WINDOWS

Thread Tools

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

Tim Golden
Posts: n/a
[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]


In short, do something like this:

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


__________________________________________________ ______________________
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:
__________________________________________________ ______________________
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
removingCR/LF from unix and windows and mixed files nntpman68 Perl Misc 3 09-11-2008 11:56 PM
compile C programs with UNIX system calls (= Unix Programs??) C Programming 18 01-10-2005 03:35 AM
compile C programs with UNIX system calls (= Unix Programs??) C++ 12 01-10-2005 03:35 AM
my own perl "dos->unix"/"unix->dos" Robert Wallace Perl Misc 7 01-22-2004 10:59 PM
Re: float incompatibility between Windows and UNIX? Alan Sung C++ 3 08-06-2003 12:31 PM