Directory String Replace Posted on April 19, 2013 by Dave Fowler
Yesterday I discovered that many of the old posts on the Chartio Blog used odd unicode versions of quotes that were being rendered terribly. So I needed to do a find-replace for an entire directory.
The tool for doing so is the sed command line utility, and not using it has a history of getting you ridiculed. After over an hour of reading man pages and online examples I discovered that I really hate sed. I asked around about other's experiences and it everyone that I talked to who had tried it has their own horror stories to go along with it.
Its probably a great tool, and I'm probably just too dumb to make it work for me and my odd unicode quote replacement job, but I ended up just writing/modifying a simple python script to do string replacing on a directory level. Its not a command line tool, or replacement for SED. Just a script that I found useful and am sharing for others that may as well.
# coding: utf-8 | |
import os, fnmatch, sys | |
def findReplace(directory, find, rep, filePattern): | |
for path, dirs, files in os.walk(os.path.abspath(directory)): | |
for filename in fnmatch.filter(files, filePattern): | |
filepath = os.path.join(path, filename) | |
with open(filepath) as f: | |
s = f.read() | |
new = s.replace(find, rep) | |
if new != s: | |
print "replaced in", filepath | |
with open(filepath, "w") as f: | |
f.write(new) |
It takes 4 parameters:
- directory - the root directory to do the string replace in
- find - the string that you would like to find
- rep - the string that you would like to replace the found string with
- filePattern - a pattern to match the files you would like searched (eg. '*.html' or '*.py')
The following example will replace all instances of the word *"old"* with *"new"* in files ending in *".html"* located in the ~/some/directory/ directory
findReplace('~/some/directory/', "old", "new", '*.html')