Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Check multiple file parms in single os.access?

112 views
Skip to first unread message

James McMahon

unread,
Apr 13, 2017, 7:31:29 AM4/13/17
to
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 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)) :

Thanks in advance for your insights. -Jim

Ben Bacarisse

unread,
Apr 13, 2017, 9:46:27 AM4/13/17
to
James McMahon <jsmcm...@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.

eryk sun

unread,
Apr 13, 2017, 2:37:08 PM4/13/17
to
On Thu, Apr 13, 2017 at 1:46 PM, Ben Bacarisse <ben.u...@bsb.me.uk> wrote:
> James McMahon <jsmcm...@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.
0 new messages