Skip to content

Instantly share code, notes, and snippets.

@Ezbob
Created October 27, 2019 13:59
Show Gist options
  • Save Ezbob/5bdf4408d319cf75f937b79bb00fa0a2 to your computer and use it in GitHub Desktop.
Save Ezbob/5bdf4408d319cf75f937b79bb00fa0a2 to your computer and use it in GitHub Desktop.
The os.walk for pathlib.Paths using generators
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