Check multiple file parms in single os.access?

Ben Bacarisse ben.usenet at bsb.me.uk
Thu Apr 13 09:46:15 EDT 2017


James McMahon <jsmcmahon3 at gmail.com> writes:

> Hello. Am a Python newbie. I have researched and found examples how we can
> check existence, readability, and write-ability on a given fully-qualified
> filename for the current python script user. Evidently os.access is the way
> to go, wrapped in some additional try and catch logic that helps insulate
> us from changes in file state after our os.access checks.

I'm not sure what you are saying here but I think it relates to a point
I was going to make...  In general, I'd say that testing for access is
not the way to go.  It is almost always better simply to try the
operations you need to do and deal with the failures as they occur.  The
bottom line is that you can't be sure the operation will work even if
os.access said it should a few moments ago.

> I understand that os.stat() calls can be relatively expensive. I
> assume likewise for os.access(). Since I'm going to apply my Python
> permission check script within a NiFi ExecuteScript processor handling
> many flowfiles, I seek any and all performance optimizations I
> possibly can.
>
> 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
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.

-- 
Ben.


More information about the Python-list mailing list