python copytree with negated ignore pattern
shutil.copytree
has an ignore
keyword. ignore
can be set to any callable. Given the directory being visited and a list of its contents, the callable should return a sequence of directory and filenames to be ignored.
For example:
import shutil
def ignored_files(adir,filenames):
return [filename for filename in filenames if not filename.endswith('foo')]
shutil.copytree(source, destination, ignore=ignored_files)
Building on unutbu's answer. The following takes a list of all files, then removes the ones matched by "ignore_patterns", then returns that as a list of files to be ignored. That is, it does a double negation to only copy the files you want.
import glob, os, shutil
def copyonly(dirpath, contents):
return set(contents) - set(
shutil.ignore_patterns('*.py', '*.el')(dirpath, contents),
)
shutil.copytree(
src='.',
dst='temp/',
ignore=copyonly,
)
print glob.glob('temp/*')