Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to get the realpath of a symbolic link?

Thread Tools

How to get the realpath of a symbolic link?

Peng Yu
Posts: n/a
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

However, os.path.abspath('b') gives me '/private/tmp/abspath/b', but
os.path.abspath('/tmp/abspath/b') gives me '/tmp/abspath/b'.
The results are not the same. So, neither os.path.abspath or
os.path.realpath gives me what I want.

I'm wondering if there is a way to get the canonical path
'/private/tmp/abspath/b', no matter whether the argument is 'b' or

#!/usr/bin/env python

import os.path

print os.path.realpath('a')
print os.path.realpath('b')
print os.path.realpath('/tmp/abspath/a')
print os.path.realpath('/tmp/abspath/b')

print os.path.abspath('a')
print os.path.abspath('b')
print os.path.abspath('/tmp/abspath/a')
print os.path.abspath('/tmp/abspath/b')
$gls -Rgtra
total 8
-rw-r--r-- 1 wheel 0 2009-10-31 01:52 a
lrwxr-xr-x 1 wheel 1 2009-10-31 01:52 b -> a
-rwx--x--x 1 wheel 312 2009-10-31 01:54
drwx------ 5 wheel 170 2009-10-31 01:54 .
drwxrwxrwt 23 wheel 782 2009-10-31 01:56 ..
$gls -lgtr /tmp
lrwxr-xr-x 1 admin 11 2009-05-21 04:28 /tmp -> private/tmp
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Re: os.path.realpath(path) bug on win7 ? Chris Rebert Python 2 01-06-2013 08:11 AM
os.path.realpath() and os.path.abspath() Jurko GospodnetiŠ Python 1 01-12-2011 03:30 AM
Re: How to get the realpath of a symbolic link? Peng Yu Python 0 10-31-2009 07:48 PM
non-OO realpath, basename, dirname? David Garamond Ruby 3 12-25-2004 05:55 AM