Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jambu/9615116 to your computer and use it in GitHub Desktop.
Save jambu/9615116 to your computer and use it in GitHub Desktop.
import subprocess
def string_replace_in_file(file_name_with_path, replacements=[]):
'''sed runs all the replacements in a single pass'''
sed_cmd = ['sed', '-i']
for l in replacements:
find_str = l['find'].replace('/', '\/')
replace_str = l['replace'].replace('/', '\/')
sed_expr = "-e s/%s/%s/g" % (find_str, replace_str)
sed_cmd += [sed_expr]
return subprocess.check_call(sed_cmd + [file_name_with_path], stderr=subprocess.STDOUT)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment