How to get the realpath of a symbolic link?

Peng Yu pengyu.ut at gmail.com
Sat Oct 31 15:24:31 EDT 2009


On Sat, Oct 31, 2009 at 1:10 PM, Emile van Sebille <emile at fenx.com> wrote:
> On 10/31/2009 10:11 AM Peng Yu said...
>>
>> 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'.
>>
>
> It still looks like it works here.  I've set up a similar structure and
> appear to get the results you're asking for using os.path.realpath.
>
> # pwd
> /home/emile
> # ls -l
> drwxr-xr-x 3 root root 4096 2009-10-31 10:25 private
> lrwxrwxrwx 1 root root   11 2009-10-31 10:25 tmp -> private/tmp
>
> # pwd
> /home/emile/tmp/abspath
> # ls -l
> -rw-r--r-- 1 root root 10 2009-10-31 10:25 a
> lrwxrwxrwx 1 root root  1 2009-10-31 10:26 b -> a
>
> Python 2.6.3 (r263:75183, Oct 15 2009, 15:03:49) [GCC 4.3.2] on linux2
>>>> import os
>>>> os.path.realpath('/home/emile/tmp/a')
> '/home/emile/private/tmp/a'
>>>> os.path.realpath('/home/emile/tmp/b')
> '/home/emile/private/tmp/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.
>
> Which is what I got above.

I'm curious why we get different results. I tried on both linux and
mac. Both of them give me the same results.



More information about the Python-list mailing list