Need function to test if EFFECTIVE UID has read-access to a file.

M

Markus Kemp

Hi all,

Well, basically the title says it all. I can't use os.access() because
it tests whether the REAL UID can read/write/exec the file. Surely
there has to be a function that does what I need, but I can't seem to
find it. Any input welcome.

Regards,

Markus
 
S

Skip Montanaro

Markus> Well, basically the title says it all. I can't use os.access()
Markus> because it tests whether the REAL UID can read/write/exec the
Markus> file. Surely there has to be a function that does what I need,
Markus> but I can't seem to find it. Any input welcome.

try:
f = open("somefile")
except IOError:
print "file not readable"

Skip
 
M

Markus Kemp

Hi Skip,

Skip Montanaro said:
try:
f = open("somefile")
except IOError:
print "file not readable"

Ow, is that the only way? =( Because as I see it, open() is a
relatively costly function compared to something like os.access(), and
since I'll be calling the function thousands of times in a row it'd be
really cool if there was something similar to os.access(). But if
there isn't I guess that'll have to do ...
Thanks for helping!

Regards,

Markus
 
D

Diez B. Roggisch

Ow, is that the only way? =( Because as I see it, open() is a
relatively costly function compared to something like os.access(), and
since I'll be calling the function thousands of times in a row it'd be
really cool if there was something similar to os.access(). But if
there isn't I guess that'll have to do ...

I didn't measure it, but I bet open internally uses the same function
os.access uses - so in case of failure, you won't have lost much, if
anything at all.

Now for the positive case: if you actually want to _do_ something with that
file, you might needed an open anyway?
 
M

Markus Kemp

Oh, and another reason why I can't use the method suggested by you is
that I'm dealing with directories, not files. Then again .... I guess
I could do something like:


try:
os.listdir( strPathname )
except:
print "No read access to pathname", strPathname, "!"


Problem is, I don't wanna catch __everything__. Does anyone know what
kind of exception is.listdir() throws if it fails to read a directory?
 
P

Peter Hansen

Markus said:
Oh, and another reason why I can't use the method suggested by you is
that I'm dealing with directories, not files. Then again .... I guess
I could do something like:

try:
os.listdir( strPathname )
except:
print "No read access to pathname", strPathname, "!"

Problem is, I don't wanna catch __everything__. Does anyone know what
kind of exception is.listdir() throws if it fails to read a directory?

Why not just set up the conditions you are interested in
and try it yourself? There are probably various different
ways to "fail to read a directory", so it would be best
if you created the specific conditions you want and tried
it out from the interactive prompt.

-Peter
 
M

Markus Kemp

Me again, haha

Okay, I've solved my problem. Rearranged my program logic and
eliminated the check that was to verify if the dir are accessable or
not, because if yes I would have called os.listdir() a few lines down
in the code anyway. So I sort of did what Skip suggested and just
listdir'd catching any OSError exceptions that get thrown if the dir
is not accessible for some reason. =)

Regards,

Markus
 
M

Mike Meyer

Hi Skip,



Ow, is that the only way? =( Because as I see it, open() is a
relatively costly function compared to something like os.access()

Yeah. you can improve on this by using os.open. That way you won't
have the overhead of creating a Python file object.

However, both access and open will have to read status information
from the disk to do their job. The I/O involved should swamp any
difference in computation between the two.

Of course, the right thing to do is time them.

BTW, os.open should work on a directory.

<mike
 
P

Peter Hansen

Mike said:
Yeah. you can improve on this by using os.open. That way you won't
have the overhead of creating a Python file object.

However, both access and open will have to read status information
from the disk to do their job. The I/O involved should swamp any
difference in computation between the two.

Of course, the right thing to do is time them.

Actually, the right thing to do is to take the simplest
and cleanest approach, and only change that if you have
hard proof that this approach is unacceptably slow or
heavy on the resources.

It seems likely that it will not be, in this case, so
just doing the file open() is almost certainly the best
thing, not to mention that it saves you all that time
doing measurements and worrying about it all...

-Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top