I have a text file which contains this information:
network={
ssid="WIFI_SSID"
scan_ssid=1
psk="WIFI_PASSWORD"
key_mgmt=WPA-PSK
}
I want to modify this text file and change the ssid and psk values. so I want something like this:
network={
ssid="KB150"
scan_ssid=1
psk="testpass"
key_mgmt=WPA-PSK
}
I wrote this code, but it only can add a new line at end of the file only for ssid (something like ssid= KB150):
if __name__ == '__main__':
ssid = "KB150"
password = "testpass"
with open("example.txt", 'r+') as outfile:
for line in outfile:
if line.startswith("ssid"):
sd = line.split("= ")
outfile.write(line.replace(sd[1], ssid))
if line.startswith("password"):
pw = line.split("= ")
line.replace(pw[1], password)
outfile.write(line.replace(pw[1], ssid))
outfile.close()
The values of ssid and psk change whenever a user enter an input in my program, so I need to find the line that starts with those keywords and change their values.