Skip to content

Instantly share code, notes, and snippets.

@non117
Created September 28, 2012 08:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save non117/3798672 to your computer and use it in GitHub Desktop.
Save non117/3798672 to your computer and use it in GitHub Desktop.
先祖path
# -*- coding: utf-8 -*-
import os
def get_ancestor_dir(path, n):
'''
pathからn階層上のディレクトリを返す
path: ディレクトリの文字列. os.path.abspath(__file__)とかを想定.
n: 遡る階層の数
return: パスの文字列
'''
return n == 0 and path.replace('\\', '/') or get_ancestor_dir(os.path.dirname(path), n - 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment