What is the use case for `os.path.normpath()`?

The docs for normpath() say:

This string manipulation may change the meaning of a path that contains symbolic links.

Are there many use cases where I can assume I won’t find any symbolic links, or can tolerate the path meaning changing? It seems niche at best, and a bit of a foot-shotgun at worst!

The only pathlib method to remove ../ entries is resolve(), which is equivalent to os.path.realpath(). There’s no equivalent to normpath() in pathlib.

Is there something to be said for deprecating normpath(), or changing it to preserve ../ entries (and therefore path meanings?)


No, normpath has its place.

The most obvious example I have to hand (a real world example) is sanity
checking some paths. I have circumstances where I want to be sure that a
computed path is sane. Examples from real code:

# the computed path is "clean"
top_asset.full_path == normpath(top_asset.full_path)

# the computed or supplied path does not try to walk out of its
# namespace
not normpath(name).startswith("..")

# the name is a local filename unless the allow_subdirs parameter is
# set
allow_subdirs or "/" not in normpath(name)

These all perform sensible logic checks on values in use. None of them
involve touching the filesystem directly at all.

Cameron Simpson cs@cskk.id.au