Check multiple file parms in single os.access?

eryk sun eryksun at gmail.com
Thu Apr 13 14:36:11 EDT 2017


On Thu, Apr 13, 2017 at 1:46 PM, Ben Bacarisse <ben.usenet at bsb.me.uk> wrote:
> James McMahon <jsmcmahon3 at gmail.com> writes:
>
>> Is there a way to mask the F_OK, R_OK, and W_OK in a single os.access
>> call? I'm guessing there must be, rather than doing this
>>
>> if ( os.access(fqfname,os.F_OK) and os.access(fqfname,os.R_OK) and
>> os.access(fqfname,os.W_OK)) :
>
> You can "or" together os.R_OK | os.W_OK into one call but you can can't

bitwise OR.

> do that with F_OK.  Mind you, I'm not sure that matters since I can't
> think of a case where you want to fail because os.F_OK fails but os.R_OK
> and os.W_OK succeeds.  In other words, why not just test for os.R_OK |
> os.W_OK?
>
> Note that despite using the "or" operation to construct the parameter,
> the test is an implied "and":
>
>   os.access(fqfname, os.R_OK | os.W_OK)
>
> is true only if both read and write access are permitted.

Note that os.access doesn't check file security on Windows. W_OK just
checks the read-only flag. Thus using try/except is really the only
possibility if you're on Windows and aren't using the Windows security
API.


More information about the Python-list mailing list