[Tutor] Re: Recursive search

sessile@in-gen.net sessile@in-gen.net
Thu, 13 Apr 2000 22:32:58 -0400

My thanks to everyone who responded so quickly!

The following was graciously provided by one of
the tutor list members and does exactly what I
wanted (it also made me slap myself a few times
for not catching on to a solution earlier)...

def getConfigs(file, visited = None):
    if not os.path.isfile(file):
        print "Not a file: %s" % file
        return []

    if visited == None:
        visited = [file]

    for line in open(file,"r").readlines():
        split = string.split(line)
        if len(split) >= 2 and split[0] == "include":
            if len(split) != 2:
                print "In file %s:" % file
                print "Illegal include syntax: %s" % line
                # Only recurse if we haven't seen it yet.
                if not split[1] in visited:
                    getConfigs(split[1], visited)
    return visited

E-Mail:  sessile@in-gen.net
  "I don't want the world... I just want your half."
                   -- TMBG (Anna Ng)