getting parent path of current directory in python Dec
10
1
0

The following code extracts the parent path of your current working directory, or any path value, in Python. I'm not sure if there's a way to automatically walk up and down through the directories, but this worked for me and was nice and small.

It works by splitting the path on "/", removes the last element in the array, then joins everything back together.

import os

path = os.getcwd()
parent_path = os.sep.join(path.split(os.sep)[:-1])
Bookmark and Share
blog comments powered by Disqus