The API for shutil.ignore_patterns () does not support absolute paths, but it is trivially easy to collapse your own option.
As a starting point, look at the source code for * ignore_patterns *:
def ignore_patterns(*patterns): """Function that can be used as copytree() ignore parameter. Patterns is a sequence of glob-style patterns that are used to exclude files""" def _ignore_patterns(path, names): ignored_names = [] for pattern in patterns: ignored_names.extend(fnmatch.filter(names, pattern)) return set(ignored_names) return _ignore_patterns
You can see that it returns a function that takes a path and a list of names, and returns a set of names to ignore. To support your use case, create your own similar function that takes advantage of the path argument. Pass your function to the ignore parameter in the copytree () call.
Alternatively, do not use shutal as-is. The source code is short and sweet, so itβs easy to cut, paste and customize.
Raymond hettinger
source share