[SOLVED] Python: Recursion and Generators

Status
You're currently viewing only Alamout's posts. Click here to go back to viewing the entire thread.
Not open for further replies.

Alamout

Ars Legatus Legionis
27,285
visit() is yielding two different things, which is messing you up. First, it yields a directory object, then it yields a generator. You should change it so it always returns one type of thing, and it needs a base case so that it terminates.

Code:
def visit(dir1):
    if os.isdir(dir1):
        for j in os.listdir(dir1):
            for k in visit(os.path.join(dir1, j)):
                 yield k
    else:
        yield dir1

That'll do a DFS on your file structure, which might not be what you want.

edit: err, yeah, his thing is the same, but uses your directory class (FYI: classes are usually capitalized). Brain's not firing on all cylinders.
 
Status
You're currently viewing only Alamout's posts. Click here to go back to viewing the entire thread.
Not open for further replies.