[Tutor] Recursive search

D-Man dsh8290@rit.edu
Thu, 13 Apr 2000 09:46:27 -0400

In your code I didn't see any line like 
	configs.append( file )

Additionally, the list is a local variable for the function.  That means
that each call to the function will have its own version of configs. 
You want to share it in some way so that all of the files will be in the
same list.  It would probably be best to add it as an argument to the

def getConfigs( file, configs = [] ):

Then pass configs to the recursive function call.  I used a default
argument here so the first time the function is called by the client,
the list will be initialized to the empty list.

This line here confuses me.  configs is a list object.  List objects
don't have a member function getConfigs().  I don't think this is what
you meant.  Maybe  configs = configs + getConfigs( split[-1] )?  If you
do this, then you wouldn't need to pass the list as an argument to the
function, but you would still need to put a configs.append( file )


sessile@in-gen.net wrote:
> Hello,
> I am reading a configuration file that can point to other
> configuration files with a similar format.  Each of those
> files can call other such files and so on.  Given the first
> file, I would like to build a list of all the config files.
> I thought a recursive function would do the trick, but I
> don't appear to be saving the list of configs properly.
> The files may be out of date so I need to test for thier
> existance.
> The code below seems to print the files out correctly,
> but the "configs" list remains empty.  Advice appreciated!
> Example files:
> <file "config.1">
> !This is a comment.
> include /path/to/some/file
> include /path/to/config.2
> =====
> <file "config.2">
> include /bad/file
> include /yet/another/file
> =====
> def getConfigs(file):
>     configs = []
>     if not os.path.isfile(file):
>         print "Not a file: %s" % file
>     else:
>         for line in open(file,"r").readlines():
>             split = map(string.strip, string.split(line))
>             try:
>                 split[0]
>             except IndexError:
>                 pass
>             else:
>                 if split[0] == "include":
>                     print split[-1]
>                     configs.getConfigs(split[-1])
>     return configs
> myconfigs = getConfigs(myconfigfile)
> --
> E-Mail:  sessile@in-gen.net
>   "I don't want the world... I just want your half."
>                    -- TMBG (Anna Ng)
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://www.python.org/mailman/listinfo/tutor