473,395 Members | 1,537 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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
Jul 18 '05 #1
8 1538

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
Jul 18 '05 #2
Hi Skip,

Skip Montanaro <sk**@pobox.com> wrote in message news:<ma**************************************@pyt hon.org>...
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
Jul 18 '05 #3
> 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?

--
Regards,

Diez B. Roggisch
Jul 18 '05 #4
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?
Jul 18 '05 #5
Markus Kemp wrote:
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
Jul 18 '05 #6
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
Jul 18 '05 #7
Nu****@gmx.net (Markus Kemp) writes:
Hi Skip,

Skip Montanaro <sk**@pobox.com> wrote in message news:<ma**************************************@pyt hon.org>...
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()


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
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 18 '05 #8
Mike Meyer wrote:
Nu****@gmx.net (Markus Kemp) writes:
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.


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
Jul 18 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: gustav04 | last post by:
hi all i have a question: what is the difference between a c-function and an c++ class method (both do exactly the same thing). lets say, i have a function called print2std() and a class...
5
by: J. J. Cale | last post by:
Little astrology program has 2 text input boxes for birth dates. There are 3 selects for day month year that will supply output to whichever text input had the focus last. If the user picks from...
19
by: Baldur Norddahl | last post by:
Hi, How come "X=null" is not the same as "X is null"? I got a few selects with queries like this: select * from foo where customer=#customer# or (#customer# is null and customer is null) ...
21
by: Johan Tibell | last post by:
I would be grateful if someone had a minute or two to review my hash table implementation. It's not yet commented but hopefully it's short and idiomatic enough to be readable. Some of the code...
176
by: nw | last post by:
Hi, I previously asked for suggestions on teaching testing in C++. Based on some of the replies I received I decided that best way to proceed would be to teach the students how they might write...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.