# # The Replace # # This is a very simple script, but i use it so often that i just # keep it here for my own reference. # import string import os import fnmatch import StringIO import shutil old_string = 'color:#FFAE00' new_string = 'color:#5c248c' files_where_to_replace = '*.html' for f in fnmatch.filter( os.listdir('.'), files_where_to_replace ): c = 0 origin = open( f, 'r' ) final = StringIO.StringIO() for line in origin: new_line = string.replace( line, old_string, new_string ) if new_line != line: c = c + 1 final.write( new_line ) origin.close() if c: shutil.copyfile( f, f + '.bak' ) fn = open( f, 'w' ) fn.write( final.getvalue() ) fn.close() print "%s replaced %i times" % ( f, c ) final.close()