diff --git a/replace_text.py b/replace_text.py new file mode 100644 index 0000000000000000000000000000000000000000..37504e7a39c3537771f8fd0b925973683c7fe358 --- /dev/null +++ b/replace_text.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +# Useful in order to edit +# a set of config files all at once + +# https://stackoverflow.com/questions/74191831/how-to-replace-a-text-with-another-text-in-several-txt-files-at-the-same-time + +import glob +import os + +# Input and output text files +pathSource = "./config_files/" +pathDest = "./config_files_new/" +textFiles_Source = glob.glob(pathSource+"*.json") +textFiles_Dest = [i.replace(pathSource,pathDest) for i in textFiles_Source] + +if not os.path.exists(pathDest): + os.makedirs(pathDest) + +# searched text and replacement text +search_text = '"nens": 20' +replace_text = '"nens": 200' + +# Loop over each text file in our list from above +for source,dest in zip(textFiles_Source,textFiles_Dest): + # Opening our text file in read only + # mode using the open() function + with open(source, 'r') as file: + + # Reading the content of the file + # using the read() function and storing + # them in a new variable + data = file.read() + + # Searching and replacing the text + # using the replace() function + data = data.replace(search_text, replace_text) + + # Opening our text file in write only + # mode to write the replaced content + with open(dest, 'w') as file: + + # Writing the replaced data in our + # text file + file.write(data)