Skip to content

Instantly share code, notes, and snippets.

@paulosuzart
Created July 26, 2010 18:17
Show Gist options
  • Save paulosuzart/490962 to your computer and use it in GitHub Desktop.
Save paulosuzart/490962 to your computer and use it in GitHub Desktop.
#!/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