views:

61

answers:

2

when i give ls -l /etc/fonts/conf.d/70-yes-bitmaps.conf

lrwxrwxrwx <snip> /etc/fonts/conf.d/70-yes-bitmaps.conf -> ../conf.avail/70-yes-bitmaps.conf

so for a symbolic link or soft link, how to find the target file's full(absolute path) in python,

If i use

os.readlink('/etc/fonts/conf.d/70-yes-bitmaps.conf')

it outputs

../conf.avail/70-yes-bitmaps.conf

but i need the absolute path not the relative path, so my desired output must be,

/etc/fonts/conf.avail/70-yes-bitmaps.conf

how to replace the .. with the actual full path of the parent directory of the symbolic link or soft link file.

+2  A: 

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.

Update:

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') 
  ) 
)
eruciform
+3  A: 
os.path.realpath(path)

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

unutbu