The folowing code is a search and replace utility in Python.
It can search directories recursively. There are several configuration flags you can set.
Users who want to replace using regular expressions need to know Python's re library.
Thanks to Bruce Dickey for pointing out a bug and fix July 3 2003.
import re #for subn
import string #for replace
import glob #for find all files of a directory
import sys #for sys.argv, sys.exit(0)
import os.path #for isdir
import os #for listdir
#############################################################################################
# Configuration section
#Global switches. Set these to 1 (on) or 0 (off)
Use_command_line_parameters = 1
Use_regular_expressions = 0 # You must be aware of Python's regular expressions to use this
Verbose = 1 # Print replaced lines in DOS box
Search_subdirectories = 1
Search_only = 0
Make_backup = 1
Backup_suffix = ".bak"
#Global variables
TempFile = "c:\\tmp\\tmp.tmp" #The temporary file
FileFilter = ["*.html"] #The type of file to modify
# Here is another example, you can add as many extensions as you like.
# All matching files will all be modified
#FileFilter = ["*.htm","*.html"]
# End configuration section
##############################################################################################
if Use_command_line_parameters:
if len(sys.argv) <> 4: # Tests the number of command line parameters
print "Usage: python replace.py " #console display
sys.exit(0) #abandon
Directory = sys.argv[1]
String_to_replace = sys.argv[2]
Replacing_string = sys.argv[3]
else: # up to you to change these strings
Directory="."
String_to_replace = "bad"
Replacing_string = "worse"
def replace_in(filename, str_to_replace, rep_string):
try:
ifh = open(filename,"r")
except:
print "cannot open",filename
sys.exit(0)
try:
ofh = open(TempFile,"w")
except:
print "cannot open",TempFile
sys.exit(0)
nsub_total = 0
while 1:
line = ifh.readline()
if not line:
break
else:
if Verbose:
if string.count(line,str_to_replace):
print line
if Use_regular_expressions:
newline,nsub=re.subn(str_to_replace, rep_string, line)
nsub_total += nsub
else:
newline=string.replace(line,str_to_replace, rep_string)
if line <> newline:
nsub_total += string.count(line, str_to_replace)
ofh.write(newline)
ifh.close()
ofh.close()
if Search_only == 0:
if nsub_total:
if Make_backup:
copy_file(filename, filename+Backup_suffix)# Do this first!!!
copy_file(TempFile,filename)
print nsub_total,"replacements done in",filename
def copy_file(from_file, to_file):
try:
ifh = open(from_file,"r")
except:
print "Cannot open",from_file
sys.exit(1)
try:
ofh = open(to_file,"w")
except:
print "Cannot open",to_file
sys.exit(2)
while 1:
line = ifh.readline()
if not line:
break
else:
ofh.write(line)
ifh.close()
ofh.close()
def search_and_replace(path):
global String_to_replace, Replacing_string,Search_subdirectories
for pattern in FileFilter:
for filename in glob.glob(path+'\\'+pattern):
print "replacing in",filename
replace_in(filename,String_to_replace,Replacing_string)
if Search_subdirectories:
for entry in os.listdir(path):
if os.path.isdir(path+"\\"+entry): # if it's a directory (could have been a file)
search_and_replace(path+"\\"+entry)
# Go!
search_and_replace(Directory)