Re: How to get the realpath of a symbolic link?

Discussion in 'Python' started by Peng Yu, Oct 31, 2009.

  1. Peng Yu

    Peng Yu Guest

    On Sat, Oct 31, 2009 at 1:46 PM, Terry Reedy <> wrote:
    > Peng Yu wrote:
    >> On Sat, Oct 31, 2009 at 11:26 AM, Emile van Sebille <>
    >> wrote:
    >>> On 10/31/2009 12:03 AM Peng Yu said...
    >>>> Suppose that I have the following directory and files. I want to get
    >>>> the canonical path of a file, a directory or a symbolic link.
    >>>> For example, for 'b' below, I want to get its canonical path as
    >>>> '/private/tmp/abspath/b'.
    >>> So, why isn't realpath working for you?  It looks like it is, and it
    >>> works
    >>> that way here:
    >>>>>> os.path.realpath('/home/emile/vmlinuz')
    >>> '/root/vmlinuz-2.4.7-10'

    >> My definition of 'realpath' is different from the definition of
    >> 'os.path.realpath'. But I'm not short what term I should use to
    >> describe. I use the following example to show what I want.
    >> In my example in the original post,
    >> '/tmp/abspath/b' is a symbolic link to '/tmp/abspath/a' and '/tmp' is
    >> a symbolic link to '/private/tmp'.
    >> Therefore, I want to get '/private/tmp/abspath/b', rather than
    >> '/private/tmp/abspath/a', as the canonical path of 'b'.
    >> If the argument is a symbolic link os.path.realpath will return the
    >> actually target of the symbolic link. However, I want the path of the
    >> symbolic link rather than the path of the target.
    >> Hope this is clear.

    > I suspect that you will have to write your own code for your own function..
    > os and os.path are written in Python, so look at the code for realpath and
    > modify it for your modified definition.

    I find the following two files that define realpath. But I don't find
    'realpath' in I looked at ''. But I don't understand how
    the function realpath is introduced in the name space in os.path.
    Would you please let me know?

    gfind . ! -path '*backup*' -name "*.py" -type f -exec grep -n "def
    realpath" {} \; -printf %p\\n\\n
    193:def realpath(path):

    345:def realpath(filename):
    Peng Yu, Oct 31, 2009
    1. Advertisements

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. Peng Yu
    Peng Yu
    Oct 31, 2009
  2. Jurko Gospodnetiæ

    os.path.realpath() and os.path.abspath()

    Jurko Gospodnetiæ, Jan 11, 2011, in forum: Python
    Adam Skutt
    Jan 12, 2011
  3. David Garamond

    non-OO realpath, basename, dirname?

    David Garamond, Dec 25, 2004, in forum: Ruby
    Ilmari Heikkinen
    Dec 25, 2004
  4. Xeno Campanoli
    Xeno Campanoli
    Mar 17, 2009
  5. Chris Rebert

    Re: os.path.realpath(path) bug on win7 ?

    Chris Rebert, Jan 6, 2013, in forum: Python
    Jan 6, 2013

Share This Page