#!/usr/bin/python import string import shutil import os import re import sys if len(sys.argv) < 3: print "usage: rename old#names new#names" print " # can be anywhere" sys.exit() nameFrom = sys.argv[1] nameTo = sys.argv[2] nameFromParts = string.split(nameFrom,"#") nameToParts = string.split(nameTo,"#") nameFromRe = "^" + nameFromParts[0] + "(.*)" + nameFromParts[1] + "$" nameToRe = "^" + nameToParts[0] + "(.*)" + nameToParts[1] + "$" print "nameFromRe",nameFromRe print "nameToRe",nameToRe for fileName in os.listdir("."): mo = re.match(nameFromRe,fileName) if mo: newFileName = nameToParts[0] + mo.group(1) + nameToParts[1] shutil.move(fileName,newFileName) print "renamed",fileName,newFileName