[Discuss] File permissions in Python

Deryk Barker dbarker at camosun.bc.ca
Sat Aug 5 15:35:12 PDT 2006


Adam Parkin wrote:

> Deryk Barker wrote:
>
>>>     $x = "foobar.txt"
>>>     print "$x is: ", ((-R $x) ? "readable " : ""),
>>>         ((-W $x) ? "writeable " : ""),
>>>         ((-x $x) ? "executable\n" : "\n");
>>
>>
>> The exact equivalent of what you're doing here would seem to be the 
>> os.access function.
>>
>> name = 'foobar.txt'
>> print name, 'is',
>> if os.access (name, os.R_OK):
>>    print 'readable',
>> if os.access (name, os.W_OK):
>>    print 'writeable',
>> if os.access (name, os.X_OK):
>>    print 'executable',
>> print
>
>
> Sweet, worked like a charm.  Thanks!  And yes, this Python code has 
> the same semantics as the Perl code (no output if no permissions set).

You can also test combinations of permissions by adding the relevant 
os.?_OK values.

Forgot to mention that earlier. (You can probably or them as well).



More information about the Discuss mailing list