def rename(root, filelist):
    """rename(root, filelist) -> None
    
    Sanitize the filenames given in 'filelist', which are rooted at 'root' by
    using a set of regex rules.
    !!! NOTE: This function calls os.tmpnam() which is insecure.
    """
    if not filelist:
        return
    def apply_rules(filename):
        rulez = [('_+'       , ' '),     # One or more underscores to spaces
                 ('-{2,}'    , '-'),     # Two or more hyphens to single hyphen
                 ('&'        , 'And'),   # An ampersand to 'And'
                 ('(-)(\w*)' ,r' \1 \2')]# Spaces around hyphen seperated words
                         
        for look_for, replacement in rulez:
            filename = re.sub(look_for, replacement, filename)
        # Capitalize first letter of every word
        filename = " ".join([ word.capitalize() for word in filename.split() ])
        return filename
    
    names = []
    for filename in filelist:
        basename = os.path.basename(filename)
        names.append(os.path.join(root, apply_rules(filename)))
    try:
        dest = os.tmpnam()
        fl = open(dest, 'w')
        fl.write("\n".join(names))
        fl.close()
        os.system("%s %s" % (EDITOR, dest))
        ans = 'no'
        for oldname, newname in zip(filelist, open(dest).readlines()):
            oldname = os.path.join(root, oldname)
            newname = newname.strip()
            if oldname == newname:
                print "No change from %s to %s ...skipping" % (oldname, newname)
            else:
                print "Changing %s to %s" % (oldname, newname)
                if not ans[0].lower == 'a':
                    ans = raw_input("Contine (Yes/No/All) ? [N] ") or 'no'
                    if ans[0].lower() in ('a', 'y'):
                        os.rename(oldname, newname)
                else:
                    os.rename(oldname, newname)
    finally:
        os.remove(dest)
rename(root, filelist) -> None
    
    Sanitize the filenames given in 'filelist', which are rooted at 'root' by
    using a set of regex rules.
    !!! NOTE: This function calls os.tmpnam() which is insecure.
    
recipes/Python/300411_foreachpy__Execute_pythfunctiselected_set/recipe-300411.py