title: Python shutil copytree: use ignore function to keep specific files types
body: <p>I'm trying to figure out how to copy CAD drawings (".dwg", ".dxf) from a source directory with subfolders to a destination directory and maintaining the original directory and subfolders structure.</p>
  
  <ul>
  <li>Original Directory: H:\Tanzania...\Bagamoyo_Single_line.dwg</li>
  <li>Source Directory: H:\CAD\Tanzania...\Bagamoyo_Single_line.dwg</li>
  </ul>
  
  <p>I found the following answer from <a href="https://stackoverflow.com/users/355230/martineau">@martineau</a> within the following post: <a href="https://stackoverflow.com/questions/35155382/copying-specific-files-to-a-new-folder-while-maintaining-the-original-subdirect">Python Factory Function</a></p>
  
  <pre><code>from fnmatch import fnmatch, filter
  from os.path import isdir, join
  from shutil import copytree
  
  def include_patterns(*patterns):
      """Factory function that can be used with copytree() ignore parameter.
  
      Arguments define a sequence of glob-style patterns
      that are used to specify what files to NOT ignore.
      Creates and returns a function that determines this for each directory
      in the file hierarchy rooted at the source directory when used with
      shutil.copytree().
      """
      def _ignore_patterns(path, names):
          keep = set(name for pattern in patterns
                              for name in filter(names, pattern))
          ignore = set(name for name in names
                          if name not in keep and not isdir(join(path, name)))
          return ignore
      return _ignore_patterns
  
  # sample usage
  
  copytree(src_directory, dst_directory,
           ignore=include_patterns('*.dwg', '*.dxf'))
  </code></pre>
  
  <p>Updated: 18:21. The following code works as expected, except that I'd like to ignore folders that don't contain any include_patterns('<em>.dwg', '</em>.dxf')</p>