Skip to content

Instantly share code, notes, and snippets.

@tstachl
Created March 12, 2011 01:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tstachl/866918 to your computer and use it in GitHub Desktop.
Save tstachl/866918 to your computer and use it in GitHub Desktop.
Search and replace a string in a file as a python one-liner
"""
Search and replace a string in a file as a python one-liner
@author Thomas Stachl
@created Mar 12, 2011
@license Attribution 3.0 Unported (CC BY 3.0)
This work is licensed under the Creative Commons Attribution 3.0
Unported License. To view a copy of this license, visit
http://creativecommons.org/licenses/by/3.0/ or send a letter to
Creative Commons, 171 Second Street, Suite 300, San Francisco,
California, 94105, USA.
"""
def replaceInFile(file, pattern, subst):
open(file, "r+").write(open(file, "r").read().replace(pattern, subst))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment