[issue21041] pathlib.PurePath.parents rejects negative indexes

victorg report at bugs.python.org
Wed Mar 4 03:13:16 EST 2020


victorg <vgordaliza at gmail.com> added the comment:

Allow negative indexes that could be usefull. 

Example: to compare 2 or more Path, if they come from the same top directory

from pathlib import Path
a = Path("/a/testpy/cpython/config.log")
b = Path("/b/testpy/cpython/config.log")
c = Path("/a/otherfolder/text.txt")
print(f"a.parents[-2] == b.parents[-2] -> {a.parents[-2] == b.parents[-2]}") # False
print(f"a.parents[-2] == c.parents[-2] -> {a.parents[-2] == c.parents[-2]}") # True 
# index = -2 because -1 is "/"

----------
nosy: +victorg
Added file: https://bugs.python.org/file48949/allowNegativeIndexParents.patch

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue21041>
_______________________________________


More information about the Python-bugs-list mailing list