Last active
November 11, 2022 06:45
-
-
Save jnrdrgz/d9473e624878ce11cbbd3523340e71da to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
def get_files(path): | |
arr = os.listdir(path) | |
print(arr) | |
return arr | |
def replace_in_files(path, words,uppers=True): | |
files = os.listdir(path) | |
for f in files: | |
print(f) | |
file_path = path+"/"+f | |
file_to_change = "" | |
with open(file_path, "r") as _f: | |
file_to_change = _f.read() | |
l1 = words[0::2] | |
l2 = words[1::2] | |
for i in range(len(l1)): | |
print("replacing {} for {}".format(l1[i], l2[i])) | |
file_to_change = file_to_change.replace(l1[i], l2[i]) | |
if uppers: | |
file_to_change=file_to_change.replace(l1[i].upper(), l2[i].upper()) | |
print(file_to_change) | |
with open(file_path, "w") as _f: | |
_f.write(file_to_change) | |
print(get_files("C:/Users/lenovo/Desktop/nsrc/replace_infolder/testfolder")) | |
pat = "C:/Users/lenovo/Desktop/nsrc/replace_infolder/testfolder" | |
replace_in_files(pat, [ | |
"insurances", "medicaments", "insurance", "medicament" | |
]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment