Skip to content

Instantly share code, notes, and snippets.

@jasonpierrepont
Last active October 8, 2022 23:19
Show Gist options
  • Save jasonpierrepont/9915fd69fe2d26588a9a to your computer and use it in GitHub Desktop.
Save jasonpierrepont/9915fd69fe2d26588a9a to your computer and use it in GitHub Desktop.
Python os.walk with DirEntry results instead of path strings. If not using python 3.5+ you'll need to have the scandir package installed. Currently, this is a simple implementation and does not fully replicate the functionality of os.walk.
try:
from os import scandir
except ImportError:
from scandir import scandir
def scantree(path):
subs = []
files = []
for entry in scandir(path):
if entry.is_dir(follow_symlinks=False):
subs.append(entry)
elif entry.is_file():
files.append(entry)
yield (path, subs, files)
for sub in subs:
for x in scantree(sub.path):
yield x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment