Created
November 13, 2014 17:42
-
-
Save mattrasband/ff1c622867b6c07f0066 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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