Skip to content

Instantly share code, notes, and snippets.

@copyleftdev
Created August 18, 2016 07:09
Show Gist options
  • Save copyleftdev/5085f42afe37f473f84ec110a564064a to your computer and use it in GitHub Desktop.
Save copyleftdev/5085f42afe37f473f84ec110a564064a to your computer and use it in GitHub Desktop.
find and replace
import fnmatch
import os
import re
import fileinput
def collect_files_lst(rootPath, patternList):
file_collection_lst = []
for each_pattern in patternList:
for root, dirs, files in os.walk(rootPath):
for filename in fnmatch.filter(files, "*.{}".format(each_pattern)):
file_collection_lst.append(os.path.join(root, filename))
print("{} Documents found.".format(len(file_collection_lst)))
return file_collection_lst
def find_replace(rootPath, patternList,txtToSearch, txtToReplace):
collected_files = collect_files_lst(rootPath, patternList)
count = 0
for each_file in collected_files:
ffile = fileinput.FileInput(each_file, inplace=True)
for line in ffile:
line.replace(txtToSearch,txtToReplace)
count += 1
print("Replaced {} accurance of {}".format(count, txtToSearch))
find_replace("testdata/game-of-life",["java"],"public","private")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment