osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Obtain the file's path.


On 9/17/19, Cameron Simpson <cs at cskk.id.au> wrote:
>
> If you just want this for your running program's internals this may not
> matter, but if you're recording the result somewhere then abspath might
> get you a more "stable" path in the above scenario.

If a path has ".." components, the abspath() result may be wrong if it
resolves them by removing a parent symlink. The absolute() method of
pathlib.Path does this right by retaining ".." components.

    >>> os.path.abspath('/foo/symlink/../bar')
    '/foo/bar'

    >>> pathlib.Path('/foo/symlink/../bar').absolute()
    PosixPath('/foo/symlink/../bar')

abspath() is also the wrong choice if we're computing the target path
for a relative symlink via relpath(). A relative symlink is evaluated
from the parsed path of its parent directory.