Skip to content

Instantly share code, notes, and snippets.

@theRobinator
Last active March 19, 2018 20:19
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 theRobinator/7dd5d37b99e03a0be01faa6d8df09709 to your computer and use it in GitHub Desktop.
Save theRobinator/7dd5d37b99e03a0be01faa6d8df09709 to your computer and use it in GitHub Desktop.
Refactor script bases
"""
This program takes a list of file names as arguments and applies a single Python function to them, in place.
An easy way to apply this to all files in a directory is:
find someDir -name '*.ts' | xargs python refactor.py
I recommend that you run this script in a clean git repository so that you can easily undo its changes using
the `git reset --hard` command.
"""
from sys import argv
def main():
for filename in argv[1:]:
with open(filename, 'r') as fp:
contents = fp.read()
new_contents = do_refactor(contents)
if new_contents != contents:
with open(filename, 'w') as fp:
fp.write(new_contents)
def do_refactor(file_contents):
# Do your stuff here
pass
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment