Skip to content

Instantly share code, notes, and snippets.

@kzahel kzahel/replace.py
Created Sep 4, 2016

Embed
What would you like to do?
replace recursive text exclude .git directory
import os, fnmatch
def findReplace(directory, find, replace, filePattern):
for path, dirs, files in os.walk(os.path.abspath(directory), topdown=True):
dirs[:] = [d for d in dirs if d not in '.git']
for filename in fnmatch.filter(files, filePattern):
filepath = os.path.join(path, filename)
with open(filepath) as f:
s = f.read()
s = s.replace(find, replace)
with open(filepath, "w") as f:
f.write(s)
#findReplace("www", "https://activities.learn-the-web.algonquindesign.ca", "http://activities.learn-the-web.tlmworks.org", "*")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.