Walking The Right Path

Discussion in 'Python' started by Tim Daneliuk, Jan 8, 2007.

  1. Tim Daneliuk

    Tim Daneliuk Guest

    Ah yes, moral philosophy and python all come together... Er, that is to day:

    Imagine you have this situation on a *nix filesystem:

    Symlink A: /foo -> /usr/home
    Symlink B: /bar -> /foo/username


    If I do this:

    import os
    print os.path.realpath("/bar")

    I get this (as one would expect):

    /usr/home/username

    However, what if I want to get back the result in this form:

    /foo/username


    IOW, is there a way to return a symlink-based path which contains
    the symlink pointer as is was *defined* not as it expands?


    TIA,
    ----------------------------------------------------------------------------
    Tim Daneliuk
    PGP Key: http://www.tundraware.com/PGP/
     
    Tim Daneliuk, Jan 8, 2007
    #1
    1. Advertising

  2. Tim Daneliuk napisal(a):
    > Ah yes, moral philosophy and python all come together... Er, that is to day:
    >
    > Imagine you have this situation on a *nix filesystem:
    >
    > Symlink A: /foo -> /usr/home
    > Symlink B: /bar -> /foo/username
    >
    >
    > If I do this:
    >
    > import os
    > print os.path.realpath("/bar")
    >
    > I get this (as one would expect):
    >
    > /usr/home/username
    >
    > However, what if I want to get back the result in this form:
    >
    > /foo/username
    >
    >
    > IOW, is there a way to return a symlink-based path which contains
    > the symlink pointer as is was *defined* not as it expands?
    >


    One way (but very ugly):
    print os.path._resolve_link('/bar')
     
    Jakub Stolarski, Jan 8, 2007
    #2
    1. Advertising

  3. Tim Daneliuk

    Tim Daneliuk Guest

    Jakub Stolarski wrote:
    > Tim Daneliuk napisal(a):
    >> Ah yes, moral philosophy and python all come together... Er, that is to day:
    >>
    >> Imagine you have this situation on a *nix filesystem:
    >>
    >> Symlink A: /foo -> /usr/home
    >> Symlink B: /bar -> /foo/username
    >>
    >>
    >> If I do this:
    >>
    >> import os
    >> print os.path.realpath("/bar")
    >>
    >> I get this (as one would expect):
    >>
    >> /usr/home/username
    >>
    >> However, what if I want to get back the result in this form:
    >>
    >> /foo/username
    >>
    >>
    >> IOW, is there a way to return a symlink-based path which contains
    >> the symlink pointer as is was *defined* not as it expands?
    >>

    >
    > One way (but very ugly):
    > print os.path._resolve_link('/bar')
    >


    Yup, that does just what I want. By why, pray tell, do you consider it
    ugly ...?


    --
    ----------------------------------------------------------------------------
    Tim Daneliuk
    PGP Key: http://www.tundraware.com/PGP/
     
    Tim Daneliuk, Jan 8, 2007
    #3
  4. Tim Daneliuk

    Tim Daneliuk Guest

    Tim Daneliuk wrote:
    > Jakub Stolarski wrote:
    >> Tim Daneliuk napisal(a):


    >>> IOW, is there a way to return a symlink-based path which contains
    >>> the symlink pointer as is was *defined* not as it expands?
    >>>

    >>
    >> One way (but very ugly):
    >> print os.path._resolve_link('/bar')
    >>

    >
    > Yup, that does just what I want. By why, pray tell, do you consider it
    > ugly ...?
    >
    >


    Whoops - that doesn't do it either. If I have this:

    /foo -> /usr/bar
    /usr/bar -> /usr1/bar

    Then realpath or _resolve_link both return "/usr1/bar" when given "/foo"
    (when what I want is "/usr/bar"). There is obviously some underlying
    OS support to do this (in this case FreeBSD 4.x) because 'ls' shows
    things as I prefer them - i.e. Links are shown as assigned not how they
    actually resolve...
    --
    ----------------------------------------------------------------------------
    Tim Daneliuk
    PGP Key: http://www.tundraware.com/PGP/
     
    Tim Daneliuk, Jan 8, 2007
    #4
  5. Tim Daneliuk

    Peter Otten Guest

    Tim Daneliuk wrote:

    > IOW, is there a way to return a symlink-based path which contains
    > the symlink pointer as is was *defined* not as it expands?


    os.readlink()

    Peter
     
    Peter Otten, Jan 8, 2007
    #5
  6. Tim Daneliuk

    Tim Daneliuk Guest

    Peter Otten wrote:
    > Tim Daneliuk wrote:
    >
    >> IOW, is there a way to return a symlink-based path which contains
    >> the symlink pointer as is was *defined* not as it expands?

    >
    > os.readlink()
    >
    > Peter


    Thanks - I couldn't find it because it was right in front of my nose :)


    --
    ----------------------------------------------------------------------------
    Tim Daneliuk
    PGP Key: http://www.tundraware.com/PGP/
     
    Tim Daneliuk, Jan 8, 2007
    #6
    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. Mikito Harakiri

    tree walking -- saved recursion state

    Mikito Harakiri, Jan 3, 2004, in forum: Java
    Replies:
    13
    Views:
    730
    Matt Humphrey
    Jan 5, 2004
  2. Tina - AffordableHOST.com

    Re: Pain while walking

    Tina - AffordableHOST.com, Jul 14, 2003, in forum: HTML
    Replies:
    0
    Views:
    400
    Tina - AffordableHOST.com
    Jul 14, 2003
  3. Tim Burch

    Re: Pain while walking

    Tim Burch, Jul 14, 2003, in forum: HTML
    Replies:
    5
    Views:
    454
    T. Audry Glamour
    Jul 16, 2003
  4. Whitecrest

    Re: Pain while walking

    Whitecrest, Jul 14, 2003, in forum: HTML
    Replies:
    5
    Views:
    1,955
    Whitecrest
    Jul 15, 2003
  5. Jim Bancroft

    XSL question-- walking a hierarchy

    Jim Bancroft, Jan 15, 2004, in forum: XML
    Replies:
    4
    Views:
    590
    Jim Bancroft
    Jan 15, 2004
Loading...

Share This Page