"Maximum recursion depth exceeded"...why?

Christian Heimes lists at cheimes.de
Wed Feb 18 01:05:57 CET 2009


Thomas Allen wrote:
> I'm referring to the same code, but with a print:
> 
> for file in os.listdir(dir):
>     if os.path.isdir(file):
>         print "D", file
> 
> in place of the internal call to absToRel...and only one line prints
> such a message. I mean, if I can't trust my OS or its Python
> implementation (on a Windows box) to recognize a directory, I'm
> wasting everyone's time here.

You are under a wrong assumption. You think os.listdir() returns a list
of absolute path elements. In fact it returns just a list of names. You
have to os.path.join(dir, file) to get an absolute path.

Anyway stop reinventing the wheel and use os.walk() as I already
explained. You can easily spot the depth with "directory.count(os.sep)".
 os.path.normpath() helps you to sanitize the path before counting the
number of os.sep.

Christian




More information about the Python-list mailing list