pathlib.Path
has an absolute
method that does what you want.
$ mkdir folder
$ touch target
$ ln -s ~/target ~/folder/link
$ ls -l folder/
total 0
lrwxrwxrwx 1 me users 16 Feb 20 19:47 link -> /home/me/target
$ cd folder
$/folder python3.7 -c 'import os.path;print(os.path.abspath("link"))'
/home/me/folder/link
$/folder python3.7 -c 'import pathlib;p = pathlib.Path("link");print(p.absolute())'
/home/me/folder/link
The method doesn't appear in the module documentation, but its docstring reads:
Return an absolute version of this path. This function works
even if the path doesn't point to anything.
No normalization is done, i.e. all '.' and '..' will be kept along.
Use resolve() to get the canonical path to a file.
It's worth noting that there are comments in the method code (in the 3.7 branch) that suggest it may not be fully tested on all platforms.