title: Python search for filename pattern within a specific directory pattern
body: <p>How can I use os.walk ( or any other way ) to search in a way such that I can locate a file with specific name under directories with certain pattern under a root directory</p>
  
  <p>What I mean is if I have a directory d:\installedApps under which I have a.ear, b.ear,... x.ear, y.ear, z.ear directories along with other directories at the same level and I want to search for a file pattern web*.xml only under the *.ear subdirectories under the root without traversing other directories at the same level, how would I do it?</p>
  
  <p>I tried various ways ( some using some other examples on this site such as walklevel example etc. ) but I do not get the results I want.</p>
  
  <p><strong>Update</strong></p>
  
  <p>I tried using the walkdepth code snippet from this site and tried to combine it in a nested loop but that doesn't work</p>
  
  <p>This is the code I tried</p>
  
  <pre><code>import os, os.path
  import fnmatch
  
  def walk_depth(root, max_depth):
      print 'root in walk_depth : ' + root
      # some initial setup for getting the depth
      root = os.path.normpath(root)
      depth_offset = root.count(os.sep) - 1
  
      for root, dirs, files in os.walk(root, topdown=True):
          yield root, dirs, files
          # get current depth to determine if we need to stop
          depth = root.count(os.sep) - depth_offset
          if depth &gt;= max_depth:
              # modify dirs so we don't go any deeper
              dirs[:] = []
  
  for root, dirs, files in walk_depth('D:\installedApps', 5):
      for dirname in dirs:
          if fnmatch.fnmatch(dirname, '*.ear'):
              print 'dirname : ' + dirname
              root2 = os.path.normpath(dirname)
              for root2, dir2, files2 in walk_depth(root2, 5):
                  for filename in files2:
                      if fnmatch.fnmatch(filename, 'webservices.xml'):
                          print '\tfilename : ' + filename
  </code></pre>