Skip to content
Snippets Groups Projects
Commit a96e498f authored by Stefano Serafin's avatar Stefano Serafin
Browse files

utility script for bulk replacement of text bits in several files

parent f4dc69f0
No related branches found
No related tags found
No related merge requests found
#!/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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment