title: Using Regex on a List of File Names Python
body: <p>I'm learning python and trying to write some utility scripts to get more familiar with it. The purpose of my script is to loop through a root directory, find files with a certain extension, and then run a regular expression on the file to categorize them. The file names are all unique but have certain common bits that correspond to a category.</p>
  
  <p>I've been able to loop through the root and print out the files (for my own test purposes) and append them to a list.</p>
  
  <pre><code>import os, glob, fnmatch
  
  rootdir = '/test/dir/subdir/'
  
  match = []
  
  for path, subdirs, files in os.walk(rootdir):
      for file in fnmatch.filter(files, '*.txt'):
          match.append(file)
          print file
  </code></pre>
  
  <p>I'd like to run a set of regular expressions on my match[] list, and if their is a match, probably add that file to another list that would be used as a subcategory for the files. Is there a module or some functionality within python to do this?</p>
  
  <p>Something along the lines of:</p>
  
  <pre><code>for file in list:
      if file.match(regex):
          do_stuff()
  </code></pre>
  
  <p>Thanks</p>