I'm trying to traverse up the directory tree like this:
from pathlib import Path
# ...
curr = Path(os.getcwd())
while True:
# do something with curr
if curr is curr.anchor:
break
curr = curr.parent
To my surprise, even though str(curr) == str(curr.anchor)
, both the expressions curr is curr.anchor
and curr == curr.anchor
return False
, so I get an infinite loop.
Is the right way to fix this really changing the condition as follows:
if str(curr) == str(curr.anchor):
break
I'm curious to know why the pathlib.Path equality operator works this way, but I'm also open to more Pythonic solutions to the main problem.