Created
May 16, 2023 11:21
-
-
Save MichaelStett/e2071e764a81b8f43cb64fcc5234fb5f to your computer and use it in GitHub Desktop.
Replace text
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 | |
path = "" | |
fileType = ".xml" | |
dictionary = { | |
"oldvalue": "newvalue", | |
} | |
modified = False | |
for dname, dirs, files in os.walk(os.path.normpath(path)): | |
for fname in files: | |
fpath = os.path.join(dname, fname) | |
if fpath.endswith(fileType) == True: | |
with open(fpath, "r", encoding="ISO-8859-1") as f: | |
s = f.read() | |
for key in dictionary: | |
if key in s: | |
s = s.replace(key, dictionary[key]) | |
modified = True | |
if modified: | |
with open(fpath, "w", encoding="ISO-8859-1") as f: | |
f.write(s) | |
modified = False | |
else: | |
print(f"{fpath} was not modified.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment