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

Discussion in 'Python' started by Markus Kemp, Nov 17, 2004.

  1. Markus Kemp

    Markus Kemp Guest

    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
     
    Markus Kemp, Nov 17, 2004
    #1
    1. Advertising

  2. Re: Need function to test if EFFECTIVE UID has read-access to afile.

    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
     
    Skip Montanaro, Nov 17, 2004
    #2
    1. Advertising

  3. Markus Kemp

    Markus Kemp Guest

    Hi Skip,

    Skip Montanaro <> wrote in message news:<>...
    > 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
     
    Markus Kemp, Nov 18, 2004
    #3
  4. > 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
     
    Diez B. Roggisch, Nov 18, 2004
    #4
  5. Markus Kemp

    Markus Kemp Guest

    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?
     
    Markus Kemp, Nov 18, 2004
    #5
  6. Markus Kemp

    Peter Hansen Guest

    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
     
    Peter Hansen, Nov 18, 2004
    #6
  7. Markus Kemp

    Markus Kemp Guest

    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
     
    Markus Kemp, Nov 18, 2004
    #7
  8. Markus Kemp

    Mike Meyer Guest

    Re: Need function to test if EFFECTIVE UID has read-access to afile.

    (Markus Kemp) writes:

    > Hi Skip,
    >
    > Skip Montanaro <> wrote in message news:<>...
    >> 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 <> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
     
    Mike Meyer, Nov 18, 2004
    #8
  9. Markus Kemp

    Peter Hansen Guest

    Mike Meyer wrote:
    > (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
     
    Peter Hansen, Nov 18, 2004
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Yan-Hong Huang[MSFT]
    Replies:
    0
    Views:
    435
    Yan-Hong Huang[MSFT]
    Oct 15, 2003
  2. Girish bharadwaj
    Replies:
    2
    Views:
    736
    Raed Sawalha
    Sep 23, 2004
  3. Raghul
    Replies:
    1
    Views:
    278
    Grant Edwards
    Feb 22, 2005
  4. Tim Arnold

    remote server and effective uid

    Tim Arnold, Nov 15, 2010, in forum: Python
    Replies:
    4
    Views:
    247
    Tim Arnold
    Nov 16, 2010
  5. Daneel Yaitskov

    how to change the effective UID

    Daneel Yaitskov, Jul 20, 2008, in forum: Perl Misc
    Replies:
    6
    Views:
    222
    Dominique Dumont
    Jul 30, 2008
Loading...

Share This Page