Python idiom to get same result as calling os.path.dirname multiple times?
Asked Answered
S

5

15

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?

Shiest answered 12/9, 2014 at 19:1 Comment(0)
B
16

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))
Breazeale answered 12/9, 2014 at 19:3 Comment(6)
That's exactly what I was looking for. As a follow up, what if any corner cases does that approach suffer from?Shiest
@TimWilder: None that I can think of right now; if you find any, let me know and I'll see how we could tackle those.Breazeale
I'm thinking of issues with symlinks; I'd need to think about the interaction between the op.join approach and paths/do some reading to explore that one more fully. The general concern is along the lines of os.path.abspath vs. os.path.realpath.Shiest
@TimWilder: realpath uses abspath under the hood, so feel free to replace the call.Breazeale
Good to know. I feel like this is an improvement on clarity and conciseness from the dirname calls, so thanks.Shiest
Wouldn't using os.pardir be more portable? Something like op.abspath(op.join(file, os.pardir, os.pardir, os.pardir))Pourpoint
P
6

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

  • get the dirname of a path that is input
  • if the level is not 0, function calls itself, causing it to get the dirname of the dirname
  • process repeats recursively until level has reached 0

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'
Pignus answered 18/1, 2018 at 21:5 Comment(1)
Short, sweet, and interpretable! Thanks!Halfandhalf
I
6

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))
Incardinate answered 5/5, 2018 at 11:34 Comment(0)
C
4
def updir(d, n):
  """Given path d, go up n dirs from d and return that path"""
  ret_val = d
  for _ in range(n):
    ret_val = os.path.dirname(ret_val)
  return ret_val

Given a directory d = '/path/to/some/long/dir' you can use the above function to go up as many levels as you want. For example:

updir(d,0)
'/path/to/some/long/dir'
updir(d,1)
'/path/to/some/long'
updir(d,2)
'/path/to/some'
updir(d,3)
'/path/to'
updir(d,4)
'/path'
Chelyabinsk answered 5/1, 2016 at 0:24 Comment(2)
Please add a little explanantion in your answer to help people understand better.Trunkfish
I added an explanation and examples.Chelyabinsk
M
0

I will extend the accepted answer with N

import os.path as op

op.abspath(op.join(__file__, \*N\*[op.pardir]))
Mansard answered 6/5, 2019 at 7:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.