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