[issue38924] pathlib paths .normalize()
Ionuț Ciocîrlan
report at bugs.python.org
Thu Nov 28 23:04:27 EST 2019
Ionuț Ciocîrlan <ionut.ciocirlan at gmail.com> added the comment:
> Can you please add an example of how normalize() should behave?
```
>>> mypath = PurePosixPath("foo/bar/bzz")
>>> mypath /= "../../"
>>> mypath
PurePosixPath('foo/bar/bzz/../..')
>>> mypath = mypath.normalize()
>>> mypath
PurePosixPath('foo')
>>> mypath /= "../../../there"
>>> mypath
PurePosixPath('foo/../../../there')
>>> mypath = mypath.normalize()
>>> mypath
PurePosixPath('../../there')
>>> mypath /= "../../and/back/again"
>>> mypath
PurePosixPath('../../there/../../and/back/again')
>>> mypath = mypath.normalize()
>>> mypath
PurePosixPath('../../../and/back/again')
```
> I assume you want the same behaviour as os.path.normpath which already accepts a pathlike object to be added to pathlib.
Yes, exactly the same behaviour, but arguing that normpath() can take a pathlib object is just saying that it saves you from doing an intermediate str(), which is, well, nice, but still not pretty. Consider `mypath = mypath.normalize()` vs. `mypath = PurePosixPath(normpath(mypath))`.
> Do note that Path inherits from PurePath, so providing a normalize() method on the latter means it will end up on the former.
That could be "circumvented" with a bit of code shuffling, e.g. moving everything from `PurePath` to a `PathBase` or `_Path` or somesuch, and forking the inheritance from there. On the other hand, it might be useful. I personally can't think of a scenario, but the GNU folk certainly think so, see `realpath --logical`: https://www.gnu.org/software/coreutils/manual/html_node/realpath-invocation.html
----------
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue38924>
_______________________________________
More information about the Python-bugs-list
mailing list