Skip to content

Instantly share code, notes, and snippets.

@kzahel
Created September 4, 2016 01:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kzahel/f160f82a2e8fbb4ed28c109470a79501 to your computer and use it in GitHub Desktop.
Save kzahel/f160f82a2e8fbb4ed28c109470a79501 to your computer and use it in GitHub Desktop.
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