I have a settings file which is called test.py. In this file I have to replace some float
, int
, string
values. eg: ORDER_PAIRS = 0.01
is a float
that i can replace with another float
value. And ORDER_CONTENT = "some string"
is a string
value. I need to read these values and replace them with new values, then overwrite the file. Like edit a settings file.
Example: I need to change ORDER_PAIRS = 0.01
to ORDER_PAIRS = 0.03
or ORDER_CONTENT = "some string"
to ORDER_CONTENT = "some new string"
.
Here is my code.
FileName = "test.py"
# Open file and replace line
with open(FileName) as f:
updatedString = f.read().replace("ORDER_PAIRS = old value", "ORDER_PAIRS = " + new value)
# Write updated string to file
with open(FileName, "w") as f:
f.write(updatedString)
How can I change certain values?