Created
July 26, 2010 18:17
-
-
Save paulosuzart/490962 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 sys | |
import shutil | |
def copyr(f,t,fnd,repl): | |
""" A simple string replace in files. You may wish to extend it for regex.""" | |
while f: | |
line = f.readline() | |
if len(line) == 0: | |
break | |
t.write(line.replace(fnd, repl)) | |
def main(fl, fnd, repl): | |
with open(fl)as pas: | |
with open('tmp', 'w+') as tmp: | |
copyr(pas, tmp, fnd, repl) | |
shutil.copy('tmp', fl) | |
if __name__ == '__main__': | |
arg = sys.argv[1:] | |
main(fl=arg[0], fnd=arg[1], repl=arg[2]) | |
# do ./reader.py ./aTextFile.txt expression_to_find expression_to_replace | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment