I want to loop through a directory with many files and append strings within a each text file. I can capture the string with re.findall method. I want to replace that string with xxxx within each text file..
import os
import pandas as pd
import numpy as np
import re
start = os.getcwd()
for (root,dirs,files) in os.walk(start):
if files.endswith('.txt'):
keepfile = files
with open(keepfiles,'a') as newFile:
content = newFile.read()
text1 = re.findall('name',content)
text2 = re.findall(‘_serial..\w+’,content)
ReplaceWith1= content .replace(text1,'xxxxxx')
ReplaceWith2= text2.replace(‘text2,’ttttt’)