I have a python file values.py
with the contents
a=10
I have another python file execute.py
with the contents
from test.values import a
def changedata():
with open("values.py",'r+') as f:
text = f.read()
new = a + 10
text = text.replace(str(a), str(new))
f.seek(0)
f.write(text)
f.truncate()
for i in range(0,4):
changedata()
When I run the execute.py
, ideally the contents of value.py
should be a=40
, but it is a=20
I fail to understand, why doesn't python change the contents of value.py
on each iteration when run in a loop. Currently, the contents of value.py
are only changed once even though it is run a in a loop.
Can someone explain this behavior and also suggest a way to fix this.