title: Iteratively (or recursively?) filter directory and copy results to new directory
body: <p>I’m new to Python and have been teaching myself with textbooks and StackOverflow. I seem to learn best from real world examples from this site. I’m hoping someone can help me with my current problem.</p>
  
  <p>I have a directory (folder) with about 1,000 files. The files have names with prefixes (patterns) such as, <code>aaa_*</code>, <code>bbb_*</code>, <code>ccc_*</code> and so on. I would like to iterate through the directory and copy all the files that have the specified prefix and move them to a new directory with that prefix name. For example, find all the <code>aaa_*</code> files in <code>/my_dir_path</code> and copy them to <code>/my_new_dir_path/aaa</code> and likewise all the <code>bbb_*</code> to <code>/my_new_dir_path/bbb</code>. The new directories have already been created.</p>
  
  <p>The following simple, boilerplate, script works for copying one of the prefix patterns to a new directory:</p>
  
  <pre><code>dir = '/my_dir_path'
  pattern = 'aaa_*'
  dest = '/my_new_dir_path/aaa'
  
  for root, dirs, files in os.walk(dir):
      for filename in fnmatch.filter(files, pattern):
          source = (os.path.join(root, filename))
          shutil.copy2(source, dest)
  </code></pre>
  
  <p>What I’m struggling with is how to recursively run <code>fnmatch.filter</code> for my list of patterns and then, ultimately, output the files to their own directory. </p>
  
  <p>I have tried writing a number of different function definitions with the list of patterns as <code>*args</code>, but would typically get an error message that “filter() takes exactly 2 arguments (3 given)”. (I apologize for not having code examples to show you here. I’ve been through so many versions in the last few days that I don’t have any one representative function definition.)</p>
  
  <p>I’m such a novice that I don’t know if <code>fnmatch.filter</code> can even be made recursive or how to devise an alternative strategy. Then, there is the problem of how to store the resulting files from each pattern search into its own directory. I was thinking that I could create a dictionary in advance, e.g.,</p>
  
  <pre><code>my_dict = {'my_new_dir_path/aaa': 'aaa_*', 'my_new_dir_path/bbb': 'bbb_*'}
  </code></pre>
  
  <p>and store the resulting files as values under the appropriate key, but given that I haven’t even gotten the recursion to work (i.e., print) I haven’t worked on how to copy the results to a new directory.</p>