Created
January 23, 2020 19:57
-
-
Save agates/51db90f77ea1a8f658906a94f9161d4a to your computer and use it in GitHub Desktop.
Wrapper around os.walk with directory/file exclusion
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 fnmatch | |
import os | |
import re | |
def exwalk(path, exclude_dirs=(), exclude_files=(), onerror=None): | |
""" | |
Wrapper around os.walk to exclude files and directories using Unix shell-style wildcards with Python's fnmatch | |
""" | |
exclude_dirs_pattern = re.compile( | |
'|'.join((fnmatch.translate(exclude_dir) for exclude_dir in exclude_dirs)), | |
flags=re.IGNORECASE) | |
exclude_files_pattern = re.compile( | |
'|'.join((fnmatch.translate(exclude_file) for exclude_file in exclude_files)), | |
flags=re.IGNORECASE) | |
for root, dirs, files in os.walk(path, onerror=onerror): | |
if len(exclude_dirs) > 0: | |
dirs[:] = [d for d in dirs if not exclude_dirs_pattern.match(d)] | |
if len(exclude_files) > 0: | |
files[:] = [f for f in files if not exclude_files_pattern.match(f)] | |
yield root, dirs, files |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment