Created
May 7, 2021 10:49
-
-
Save leafsummer/8587caa398a386297300a0ed1c958fab to your computer and use it in GitHub Desktop.
[walk up directory for list all files]
This file contains 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
def walk_up(bottom): | |
"""mimic os.walk, but walk 'up' instead of down the directory tree. | |
From: https://gist.github.com/zdavkeos/1098474 | |
""" | |
bottom = os.path.realpath(bottom) | |
# get files in current dir | |
try: | |
names = os.listdir(bottom) | |
except Exception: | |
return | |
dirs, nondirs = [], [] | |
for name in names: | |
if os.path.isdir(os.path.join(bottom, name)): | |
dirs.append(name) | |
else: | |
nondirs.append(name) | |
yield bottom, dirs, nondirs | |
new_path = os.path.realpath(os.path.join(bottom, "..")) | |
# see if we are at the top | |
if new_path == bottom: | |
return | |
for x in walk_up(new_path): | |
yield x |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment