how to find the target file's full(absolute path) of the symbolic link or soft link in python how to find the target file's full(absolute path) of the symbolic link or soft link in python python python

how to find the target file's full(absolute path) of the symbolic link or soft link in python


os.path.realpath(path)

os.path.realpath returns the canonical path of the specified filename, eliminating any symbolic links encountered in the path.


As unutbu says, os.path.realpath(path) should be the right answer, returning the canonical path of the specified filename, resolving any symbolic links to their targets. But it's broken under Windows.

I've created a patch for Python 3.2 to fix this bug, and uploaded it to:

http://bugs.python.org/issue9949

It fixes the realpath() function in Python32\Lib\ntpath.py

I've also put it on my server, here:

http://www.burtonsys.com/ntpath_fix_issue9949.zip

Unfortunately, the bug is present in Python 2.x, too, and I know of no fix for it there.


http://docs.python.org/library/os.path.html#os.path.abspath

also joinpath() and normpath(), depending on whether you're in the current working directory, or you're working with things elsewhere. normpath() might be more direct for you.

Specifically:

os.path.normpath(   os.path.join(     os.path.dirname( '/etc/fonts/conf.d/70-yes-bitmaps.conf' ),     os.readlink('/etc/fonts/conf.d/70-yes-bitmaps.conf')   ) )