Problem in directory working

Gyoung-Yoon Noh nohmad at sub-port.net
Sun Jan 11 08:37:25 CET 2004


I've written Unix's find-like function using recursive os.listdir()
call and generator expression. It seems that error happens in
generator expression.
Below codes do not work. walk.next() generates StopIteration.
When I replace 'yield' with 'print', it works fine.

Why this does not work?
I've had successively implemented unix-find using os.path.walk().
But I want to combine recursive call and generator expressions.

Any comments are welcome.


[snip]
import os

def find(path):
    if not os.path.exists(path):
        raise StopIteration
    if os.path.isdir(path):
        for l in os.listdir(path):
            fullpath = os.sep.join((path, l))
            find(fullpath)
    else:
        yield path

if __name__=='__main__':
    #find('/home/myhome'); raise SystemExit()
    walk = find('/home/myhome')
    for i in walk:
        print i.next()



More information about the Python-list mailing list