I find myself needing to get a parent directory of a python file in a source tree that is multiple directories up with some regularity. Having to call dirname many times is clunky.
I looked around and was surprised to not find posts on this.
The general scenario is:
import os.path as op
third_degree_parent = op.dirname(op.dirname(op.dirname(op.realpath(__file__))))
Is there a more idiomatic way to do this that doesn't require nested dirname calls?
Someone else added an answer in 2018, 4 years later, so why not add mine. The other answers are either long or become long if a larger number of parents are required. Let's say you need 7 parents. This is what I do
os.path.abspath(__file__ + 8 * '/..')
Note the extra (8=7+1) to remove 7 parents as well as the file name. No need for os.path.pardir
as abspath
understands /..
universally and will do the right thing. Also has the advantage that the number of parents can be dynamic, determined at run-time.
In comparison, the equivalent using the accepted answer (longer and less obvious):
import os.path as op
op.abspath(op.join(__file__, op.pardir, op.pardir, op.pardir, op.pardir, op.pardir, op.pardir, op.pardir))
Normalize a relative path; os.pardir
is the parent directory, repeat it as many times as needed. It is available via os.path.pardir
too:
import os.path as op
op.abspath(op.join(__file__, op.pardir, op.pardir, op.pardir))
Since it has not been demonstrated yet, here is an answer using a recursive function.
Function
import os
def parent(path, level = 0):
parent_path = os.path.dirname(path)
if level == 0:
return parent_path
return parent(parent_path, level - 1)
Explaination
Example
>>> parent('/my/long/path/name/with/a/file.txt')
'/my/long/path/name/with/a'
>>> parent('/my/long/path/name/with/a/file.txt', 0)
'/my/long/path/name/with/a'
>>> parent('/my/long/path/name/with/a/file.txt', 4)
'/my/long'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With