"Maximum recursion depth exceeded"...why?

MRAB google at mrabarnett.plus.com
Tue Feb 17 19:04:08 EST 2009


Thomas Allen 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):

os.listdir() returns a list of filenames, not filepaths. Create the
filepath with os.path.join(dir, filename).

>         if os.path.isdir(filename):
>             absToRel(filename, root)
>         else:
>             if(filename.endswith("html") or filename.endswith("htm")):
>                 print filename
> 
> if __name__ == "__main__":
>     main()
> 




More information about the Python-list mailing list