My goal: search webapps folder to find all .war files and make sure a folder exists with the same name(indicating .war expansion)

My Code:

import os
import stat

file = open("hosts.txt","r")

for servername in file:
    path = '\\\\'+servername.strip()+'\\d$\\tomcat\\Servers\\'
    server_name = servername.strip()
    for r,d,f in os.walk(path, topdown=False):

        for files in f:
            print os.path.join(r,files)
            if files.endswith(".war"):
                print "FILE LOCATION: " + os.path.join(r,files)+ " FILE SIZE: " +str(statinfo.st_size)
                print os.path.join(r,files)
                folder= os.path.join(r,files).replace('.war','')
                if os.path.isdir(folder):
                    print "folder missing:",folder

My problem:
on each server i'm searching the directory structure is such:

my search is taking forever because i can't put a wildcard in at \servername\ to skip down to the webapps folder where the .war files exist. If i do this with 200 vm's then the search will be quite expensive.

the folder missing print indicates to me the .war file did not expand because there's no folder matching.

Would someone know of a way to search the specific directory or now to skip a directory?

Thank you

You can use a breadth-first traversal to find the webapps folders first, then walk these folders

import collections as col
import itertools as itt
import os

def breadth_first(root, subnodes_func, max_depth=None):
    """Yield pairs (depth, node) in the breadth-first traversal of a tree

            root: root node of the tree
            subnodes_func: function of one node argument returning
                a sequence of subnodes
            max_depth: maximum depth of generated nodes (defaults to None)
    fifo = col.deque([(0, root)])
    limited = (max_depth is not None)
    while fifo:
        depth, node = fifo.popleft()
        yield depth, node
        if limited and depth >= max_depth:
        depth += 1
        for subnode in subnodes_func(node):
            fifo.append((depth, subnode))

def subdirs(dir):
    """Generates subfolders of a given folder"""
        r, d, f = next(os.walk(dir))
        for x in d:
            yield os.path.join(r, x)
    except StopIteration:

def gen_webapps_dirs(root, max_depth=None):
    """Generate folders named 'webapps' while traversing root folder breadth-first"""
    for d, dir in breadth_first(root, subdirs, max_depth=max_depth):
        if os.path.basename(dir) == 'webapps':
            yield dir

if __name__ == '__main__':
    file = open("hosts.txt","r")

    for servername in file:
        path = '\\\\'+servername.strip()+'\\d$\\tomcat\\Servers\\'
        for webapps in gen_webapps_dirs(path, max_depth=2):


Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.