def remove_dead_links(directory, verbose=0):
 for (dirpath, dirname, filenames) in walk(directory):
  for filename in (dirnames + filenames):
   src = join(dirpath, filename)
   if (islink(src) and (not exists(src))):
    if verbose:
     print('remove dead link', src)
    remove(src)

def remove_dead_links ( directory , verbose = 0 ) : for ( dirpath , dirname , filenames ) in walk ( directory ) : for filename in ( dirnames + filenames ) : src = join ( dirpath , filename ) if ( islink ( src ) and ( not exists ( src ) ) ) : if verbose : print ( 'remove dead link' , src ) remove ( src )
recursively traverse directory and remove all dead links .