Skip to content

Instantly share code, notes, and snippets.

@mattrasband
Created November 13, 2014 17:42
Show Gist options
  • Save mattrasband/ff1c622867b6c07f0066 to your computer and use it in GitHub Desktop.
Save mattrasband/ff1c622867b6c07f0066 to your computer and use it in GitHub Desktop.
# Similar to os.path.walk but traverses up the tree
def walkup(bottom=os.path.dirname(__file__)):
"""Similar to os.walk, but up the tree
instead of down.
"""
path = os.path.realpath(bottom)
dirs = []
files = []
for item in os.listdir(path):
abs_path = os.path.join(path, item)
if os.path.isdir(abs_path):
dirs.append(item)
else:
files.append(item)
yield (path, dirs, files)
up = os.path.realpath(os.path.join(bottom, '..'))
if up == bottom:
return
for x in walkup(up):
yield x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment