I have a file at location a/b/c/d/e/f/x.xml
. i need to find the absolute path of the the dir d
, i.e a parent dir up the hierarchy that matches the dir name d
.
I can obtain the filename's current dir as os.path.abspath(__file__)
. i have see the documentation for pathlib and glob, but am unable to figure out how would i use them.
Can someone help
EDIT:
Thanks to all the answers below, I have gotten to a one liner
os.path.join(*list(itertools.takewhile(lambda x: x != 'd', pathlib.PurePath(os.getcwd()).parts)))
I also need to append the actual dir name to it, i.e, the output should be a/b/c/d
. An ugly solution is below (uses os.path.join twice). Can someone fix it (by adding an element to the iterator or to the list in one line :)
os.path.join(os.path.join(*list(itertools.takewhile(lambda x: x != 'd', pathlib.PurePath(os.getcwd()).parts))),"d")