recursive traversal of file

Paul McGuire ptmcg at austin.rr.com
Tue Aug 19 14:08:29 CEST 2003


To prevent a recursive infinite loop, keep a list of the current chain of
filenames being imported.  Before importing filename_from_pattern, make sure
it is not in the list (else you will eventually loop through the same files
until your stack blows up or similar dire consequence).  If the file is in
the list, you can skip it, or raise an exception.

-- Paul McGuire
Austin, TX

>
> This *should* work, although I didn't test it.  Also, it assumes that
recursive
> function calls share the same list ('data').  It would be cleaner to have
> import_file create, fill and return its own list.  In pseudocode:
>
> def import_file(filename):
>      data = []
>      ...
>      for line in lines:
>         if (pattern matches):
>             z = import_file(filename_from_pattern)
>             data.extend(z)
>         ...
>
>      return data
>
>






More information about the Python-list mailing list