Skip to content

Instantly share code, notes, and snippets.

@shavidzet
Last active June 30, 2018 08:20
Show Gist options
  • Save shavidzet/9f20e734d118dbf81e70f32c18d81e03 to your computer and use it in GitHub Desktop.
Save shavidzet/9f20e734d118dbf81e70f32c18d81e03 to your computer and use it in GitHub Desktop.
import os, fnmatch
def findReplace(directory, find, replace, 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()
s = s.replace(find, replace)
with open(filepath, "w") as f:
f.write(s)
findReplace("/dir", "foo", "bar", "*.js")
find ./ -type f -exec sed -i ‘s/string1/string2/’ {} \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment