Skip to content

Instantly share code, notes, and snippets.

@justnom
Created August 30, 2013 16:48
Show Gist options
  • Save justnom/6391928 to your computer and use it in GitHub Desktop.
Save justnom/6391928 to your computer and use it in GitHub Desktop.
Ignore function generator for `shutil.copytree` function.
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'))
@bdillahu
Copy link

bdillahu commented Jun 8, 2016

Thank you! Tearing my hair out to get this working...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment