massage file handling
This commit is contained in:
parent
e541a885f5
commit
fc1267fe55
@ -28,15 +28,17 @@ def replaceInFile(in_file, out_file, text, subs, flags=0):
|
|||||||
Taken from https://www.studytonight.com/python-howtos/search-and-replace-a-text-in-a-file-in-python
|
Taken from https://www.studytonight.com/python-howtos/search-and-replace-a-text-in-a-file-in-python
|
||||||
"""
|
"""
|
||||||
if os.path.exists(in_file):
|
if os.path.exists(in_file):
|
||||||
with open(in_file, "rb") as infile:
|
#read the file contents
|
||||||
with open(out_file, "wb") as outfile:
|
with open(in_file, "r", encoding="utf-8") as infile:
|
||||||
#read the file contents
|
file_contents = infile.read()
|
||||||
file_contents = infile.read()
|
|
||||||
text_pattern = re.compile(re.escape(text), flags)
|
# do replacement
|
||||||
file_contents = text_pattern.sub(subs, file_contents.decode('utf-8'))
|
text_pattern = re.compile(re.escape(text), flags)
|
||||||
outfile.seek(0)
|
file_contents = text_pattern.sub(subs, file_contents)
|
||||||
outfile.truncate()
|
|
||||||
outfile.write(file_contents.encode())
|
# write the result
|
||||||
|
with open(out_file, "w", encoding="utf-8") as outfile:
|
||||||
|
outfile.write(file_contents)
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
if (env.GetProjectOption('custom_patches', '') == ''):
|
if (env.GetProjectOption('custom_patches', '') == ''):
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user