Is there a way I can filter a directory by using the absolute path to it?
shutil.copytree(directory,
target_dir,
ignore = shutil.ignore_patterns("/Full/Path/To/aDir/Common"))
This doesn't seem to work when trying to filter the "Common" Directory located under "aDir
". If I do this:
shutil.copytree(directory,
target_dir,
ignore = shutil.ignore_patterns("Common"))
It works, but every directory called Common will be filtered in that "tree", which is not what I want.
Any suggestions ?
Thanks.
The API for shutil.ignore_patterns() doesn't support absolute paths, but it is trivially easy to roll your own variant.
As a starting point, look at the source code for *ignore_patterns*:
You can see that it returns a function that accepts a path and list of names, and it returns a set of names to ignore. To support your use case, create you own similar function that uses takes advantage of path argument. Pass your function to the ignore parameter in the call to copytree().
Alternatively, don't use shutil as-is. The source code is short and sweet, so it isn't hard to cut, paste, and customize.
You'll want to make your own ignore function, which checks the current directory being processed and returns a list containing 'Common' only if the dir is '/Full/Path/To/aDir'.
You can make your own ignore function:
Or, if you want to be able to call
copytree
with a relative path:From the docs: