problem with os.path.walk

Jiba a11w at SoftHome.net
Thu Aug 9 08:34:01 EDT 2001


Hi,

I've just experimented a problem with os.path.walk, as defined in the
posixpath.py file (other implementations may have similar behaviour) in
Python 2.0 :

def walk(top, func, arg):
    try:
        names = os.listdir(top)
    except os.error:
        return
    func(arg, top, names)
    for name in names:
            name = join(top, name)
            st = os.lstat(name)
            if stat.S_ISDIR(st[stat.ST_MODE]):
                walk(name, func, arg)

if the call to the user function "func" delete one of the file in
"names", the call to "os.lstat" will fail...

what about walking in the subdirectories BEFORE walking in the top
directory :

def walk(top, func, arg):
    try:
        names = os.listdir(top)
    except os.error:
        return
    for name in names:
            name = join(top, name)
            st = os.lstat(name)
            if stat.S_ISDIR(st[stat.ST_MODE]):
                walk(name, func, arg)
    func(arg, top, names) # Moved at the end !

Thank you for any comment,

Jiba




More information about the Python-list mailing list