Obtain the file's path.

Cameron Simpson cs at cskk.id.au
Wed Sep 18 05:38:21 EDT 2019


On 18Sep2019 03:36, eryk sun <eryksun at gmail.com> wrote:
>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.

For the record, I agree entirely with Eryk here.

Cheers,
Cameron Simpson <cs at cskk.id.au>



More information about the Python-list mailing list