def safe_walk(top, topdown=True, onerror=None, followlinks=True, _seen=None):
 (islink, join, isdir) = (os.path.islink, os.path.join, os.path.isdir)
 if (_seen is None):
  _seen = set()
 try:
  names = os.listdir(top)
 except os.error as err:
  if (onerror is not None):
   onerror(err)
  return
 if followlinks:
  status = os.stat(top)
  if (status.st_ino != 0):
   node = (status.st_dev, status.st_ino)
   if (node in _seen):
    return
   _seen.add(node)
 (dirs, nondirs) = ([], [])
 for name in names:
  full_path = join(top, name)
  if isdir(full_path):
   dirs.append(name)
  else:
   nondirs.append(name)
 if topdown:
  (yield (top, dirs, nondirs))
 for name in dirs:
  new_path = join(top, name)
  if (followlinks or (not islink(new_path))):
   for x in safe_walk(new_path, topdown, onerror, followlinks, _seen):
    (yield x)
 if (not topdown):
  (yield (top, dirs, nondirs))

def safe_walk ( top , topdown = True , onerror = None , followlinks = True , _seen = None ) : ( islink , join , isdir ) = ( os . path . islink , os . path . join , os . path . isdir ) if ( _seen is None ) : _seen = set ( ) try : names = os . listdir ( top ) except os . error as err : if ( onerror is not None ) : onerror ( err ) return if followlinks : status = os . stat ( top ) if ( status . st_ino != 0 ) : node = ( status . st_dev , status . st_ino ) if ( node in _seen ) : return _seen . add ( node ) ( dirs , nondirs ) = ( [ ] , [ ] ) for name in names : full_path = join ( top , name ) if isdir ( full_path ) : dirs . append ( name ) else : nondirs . append ( name ) if topdown : ( yield ( top , dirs , nondirs ) ) for name in dirs : new_path = join ( top , name ) if ( followlinks or ( not islink ( new_path ) ) ) : for x in safe_walk ( new_path , topdown , onerror , followlinks , _seen ) : ( yield x ) if ( not topdown ) : ( yield ( top , dirs , nondirs ) )
just like os .