Created
May 11, 2012 19:42
-
-
Save larsks/2661995 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
#!/usr/bin/python | |
import os | |
import sys | |
import argparse | |
def parse_args(): | |
p = argparse.ArgumentParser() | |
p.add_argument('--dryrun', '-n', action='store_true') | |
p.add_argument('search') | |
p.add_argument('replace') | |
p.add_argument('files', nargs='*') | |
return p.parse_args() | |
def main(): | |
opts = parse_args() | |
search = open(opts.search).read() | |
replace = open(opts.replace).read() | |
for file in opts.files: | |
with open(file) as f: | |
data = f.read() | |
if search in data: | |
print file | |
if opts.dryrun: | |
continue | |
while search in data: | |
i = data.find(search) | |
fixed = data[:i] + \ | |
replace + \ | |
data[i + len(search):] | |
data = fixed | |
with open(file, 'w') as f: | |
f.write(fixed) | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment