def renamer(files, pattern, replacement):
    for pathname in glob.glob(files):
        basename = os.path.basename(pathname)
new_filename = re.sub(pattern, replacement, basename)
if new_filename != basename:
    os.rename(pathname, os.path.join(os.path.dirname(pathname), new_filename))
Batch Renaming of Files in a Directory