Python copytree with negative ignore pattern

I am trying to use python to copy a file / directory tree.

Is it possible to use copytree to copy everything that ends in foo?

There is a function ignore_patterns patterns, can I give it a negative regular expression? Are they supported in python?

eg.

copytree (src, dest, False, ignore_pattern ('! *. foo')) Where! does NOT mean something that ends in foo. thank.

+5
source share
3 answers

shutil.copytree ignore. ignore . , , .

:

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)
+7

unutbu. , , "ignore_patterns", , . , , .

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/*')
+4
def documentation(format):
    call(['make', format, '-C', DOC_SOURCE_DIR])

    if (os.path.exists(DOC_DIR)):
        shutil.rmtree(DOC_DIR)

    ignored = ['doctrees']
    shutil.copytree('{0}/build/'.format(DOC_SOURCE_DIR), DOC_DIR, ignore=shutil.ignore_patterns(*ignored))
0
source

All Articles