Skip to content

Instantly share code, notes, and snippets.

@fredrikaverpil
Created March 25, 2014 13:07
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 fredrikaverpil/9761449 to your computer and use it in GitHub Desktop.
Save fredrikaverpil/9761449 to your computer and use it in GitHub Desktop.
Walk folder and search and replace in files #python
import os
startDirectory = "c:/temp/"
searchString = "maya2012"
replacement = """multiline
string"""
for dname, dirs, files in os.walk(startDirectory):
for fname in files:
if ".ma" in fname:
fpath = os.path.join(dname, fname)
print "Reading " + fpath
with open(fpath) as f:
s = f.read()
s = s.replace(searchString, replacement)
with open(fpath, "w") as f:
f.write(s)
print "Wrote " + fpath
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment