Created
March 18, 2014 07:24
-
-
Save jambu/9615116 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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