Python search for filename pattern within a specific directory pattern

17,490

I would highly recommend looking at this answer. There are three different solutions but #1 seems like it most accurately matches what you are trying to do.

Find all files in a directory with extension .txt in Python

EDIT I just found some more information on the glob class that may do the job.

From Python Docs

glob.glob(pathname)

Return a possibly-empty list of path names that match pathname, which must be a string containing a path specification. pathname can be either absolute (like /usr/src/Python-1.5/Makefile) or relative (like ../../Tools/*/*.gif), and can contain shell-style wildcards. Broken symlinks are included in the results (as in the shell).

So you could do something along the lines of:

def getFiles(path):
    answer = []
    endPaths = glob.glob(path + "web*.xml")
    answer += endPaths
    if len(glob.glob(path + "*ear/")) > 0:
            answer += getFiles(path + "*ear/")

    return answer

filepaths = getFiles("./")
print(filepaths

)

I actually tested that one and it work wonderfully in a directory that I think is set up the way you want.

Share:
17,490
adbdkb
Author by

adbdkb

Merge me

Updated on June 05, 2022

Comments

  • adbdkb
    adbdkb almost 2 years

    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

    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?

    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.

    Update

    I tried using the walkdepth code snippet from this site and tried to combine it in a nested loop but that doesn't work

    This is the code I tried

    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 >= 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