[Python-ideas] Three ways of paths canonization

Serhiy Storchaka storchaka at gmail.com
Wed Sep 7 05:20:10 EDT 2016


Before removing provisional state from the pathlib module, we should 
resolve the issue with Path.resolve(). It corresponds to 
os.path.realpath(), but behaves differently in case of non-existent 
path. Actually we can't say that any of these functions is wrong. Both 
behaviors make sense in different situations.

The readlink utility from GNU coreutils has three mode for resolving 
file path:

        -f, --canonicalize
               canonicalize by following every symlink in every 
component of the given name recursively; all but the last component must 
exist

        -e, --canonicalize-existing
               canonicalize by following every symlink in every 
component of the given name recursively, all components must exist

        -m, --canonicalize-missing
               canonicalize by following every symlink in every 
component of the given name recursively, without requirements on 
components existence

Current behavior of posixpath.realpath() is matches (besides one minor 
detail) to `readlink -m`. The behavior of Path.resolve() matches 
`readlink -e`.

I have proposed a patch that adds three-state optional parameter to 
posixpath.realpath() and I'm going to provide similar patch for 
Path.resolve(). But I'm not sure this is good API. Are there better 
variants?

[1] http://bugs.python.org/issue19717
[2] http://bugs.python.org/issue27002



More information about the Python-ideas mailing list