I want to replace a specific string in a file by a variable's value:
In my file there is these 2 lines:
<CtrlSum>10</CtrlSum>
<sum>45</sum>
I want to search for the value "10" and replace it by "45".
I tried something like that:
with open(path,'r+') as f:
lst=f.readlines()
for j in lst:
if'sum' in j:
Somme = j.split('>')[1].split('<')[0]
print(Somme)
for i in lst:
if 'CtrlSum' in i:
Ctrl = i.split('>')[1].split('<')[0]
print (Ctrl)
f.writelines(f.replace(Ctrl,Somme))
But I have this error:
f.writelines(f.replace(Ctrl,Somme)) AttributeError: '_io.TextIOWrapper' object has no attribute 'replace'
What is wrong and how can I fix it?