Created
August 30, 2013 16:48
-
-
Save justnom/6391928 to your computer and use it in GitHub Desktop.
Ignore function generator for `shutil.copytree` function.
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
def ignore_patterns_relative(*patterns): | |
"""Function that can be used as copytree() ignore parameter. | |
Patterns used can exclude with relative path names. | |
""" | |
def _ignore_patterns(path, names): | |
ignored_names = [] | |
for pattern in patterns: | |
relative_names = [os.path.join(path, name) for name in names] | |
matched_names = fnmatch.filter(relative_names, pattern) | |
ignored_names.extend(os.path.basename(name) for name in matched_names) | |
return set(ignored_names) | |
return _ignore_patterns | |
# USAGE | |
from shutil import copytree | |
#Only ignore text files found in the first level directory | |
copytree('/home/from/', '/home/to/', ignore=ignore_patterns_relative('/home/from/*.txt')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you! Tearing my hair out to get this working...