"Maximum recursion depth exceeded"...why?

Thomas Allen thomasmallen at gmail.com
Tue Feb 17 23:08:39 CET 2009


On Feb 17, 4:46 pm, Thomas Allen <thomasmal... at gmail.com> wrote:
> I must not be understanding something. This is a simple recursive
> function that prints all HTML files in argv[1] as its scans the
> directory's contents. Why do I get a RuntimeError for recursion depth
> exceeded?
>
> #!/usr/bin/env python
>
> import os, sys
>
> def main():
>     absToRel(sys.argv[1], sys.argv[2])
>
> def absToRel(dir, root):
>     for filename in os.listdir(dir):
>         if os.path.isdir(filename):
>             absToRel(filename, root)
>         else:
>             if(filename.endswith("html") or filename.endswith("htm")):
>                 print filename
>
> if __name__ == "__main__":
>     main()

Please note that I'm not using os.walk(sys.argv[1]) because the
current depth of recursion is relevant to the transformation I'm
attempting. Basically, I'm transforming a live site to a local one and
the live site uses all absolute paths (not my decision...). I planned
on performing the replace like so for each line:

line.replace(root, "../" * depth)

So that a file in the top-level would simple remove all instances of
root, one level down would sub "../", etc.



More information about the Python-list mailing list