Last active
October 8, 2022 23:19
-
-
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.
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
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