Skip to content

Instantly share code, notes, and snippets.

@minikomi
Created December 18, 2015 09:14
Show Gist options
  • Save minikomi/661867533d24b8d82429 to your computer and use it in GitHub Desktop.
Save minikomi/661867533d24b8d82429 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# -*- coding: utf-8 -*-
import glob
from tempfile import mkstemp
from shutil import move
from os import remove, close
import re
def replace(file_path, pattern, subst):
fh, abs_path = mkstemp()
with open(abs_path, 'w') as new_file:
with open(file_path) as old_file:
new_file.write(re.sub(pattern, subst, old_file.read()))
close(fh)
remove(file_path)
move(abs_path, file_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment