Skip to content

Instantly share code, notes, and snippets.

@GMadorell
Created October 7, 2014 22:23
Show Gist options
  • Save GMadorell/e6dc4d13c3712edc6b01 to your computer and use it in GitHub Desktop.
Save GMadorell/e6dc4d13c3712edc6b01 to your computer and use it in GitHub Desktop.
Pathing. Easy way to evade the pathing problems when running an script in python.
import os
TOP_LEVEL_NAME = "lorayne_numbers"
def __get_current_path():
return os.path.dirname(os.path.abspath(__file__))
def get_top_level_path():
path = __get_current_path()
head, tail = os.path.split(path)
while tail != TOP_LEVEL_NAME:
head, tail = os.path.split(path)
return os.path.join(head, tail)
def get_path_relative_to_top_level(*args):
"""""
:param args: Pass directories followed by an optional file at the end.
"""
path = get_top_level_path()
for next_step in args:
path = os.path.join(path, next_step)
return path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment