Created
October 27, 2019 13:59
-
-
Save Ezbob/5bdf4408d319cf75f937b79bb00fa0a2 to your computer and use it in GitHub Desktop.
The os.walk for pathlib.Paths using generators
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
import pathlib | |
def path_walk(top_path): | |
if not top_path.is_dir(): return top_path | |
top_queue = [top_path] # using list for a queue here, but any collection with the same offer / pop can do | |
while len(top_queue) != 0: | |
current_dir = top_queue[0] | |
for filepath in current_dir.iterdir(): | |
if filepath.is_dir(): | |
top_queue.append(filepath) | |
elif filepath.is_file(): | |
yield filepath | |
top_queue.pop(0) | |
if __name__ == "__main__": | |
path = pathlib.Path(".") | |
lua_files_from_the_current_dir_and_down = (p for p in path_walk(path) if p.suffix == ".lua") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment