3

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
adbdkb
  • 1,897
  • 6
  • 37
  • 66

1 Answers1

4

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.

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
Steven
  • 240
  • 1
  • 7
  • Thanks. I did look at that for doing the pattern for filename. I guess I am having trouble with nesting it in such a way that I don't have to traverse all the directories at the root level but look only in *.ear directories and then look for specific files only in those directories. – adbdkb Dec 03 '13 at 18:21
  • Oh I'm sorry I guess that was in your question. I feel like that would have to be done manually which I don't have too much experience with file traversing in Python. I may get back to you later with a new solution but I'm kind of busy at work now. – Steven Dec 03 '13 at 18:31
  • 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 fname in files2: if fnmatch.fnmatch(fname, 'webservices.xml'): print '\tfilename : ' + filename – adbdkb Dec 03 '13 at 18:37
  • Just thought I'd let you know I found an answer that works with my understanding of your problem. – Steven Dec 03 '13 at 19:06
  • Thank you for your efforts. My problem is - under the ear directory, I do not know what level subdirectory my file may be in, so i need to traverse the ear directory once it is found. Does it make sense? – adbdkb Dec 03 '13 at 19:35
  • 1
    Oh I'm sorry I really thought you said just the one depth. I am having a hard time focusing today :p. Anyway there is a recursive solution for you. You can make it more efficient/modular or what have you I just wanted what the code to be clear. – Steven Dec 03 '13 at 19:56