(Nov-04-2021, 12:53 AM)bowlofred Wrote: from mqtt_as import MQTTClient, config
ssid, password = get_from_wifimgr()
config["ssid"] = ssid
config["ssid_pw"] = password
client = MQTTClient(config)
Thanks but in my case it won't work.
For an easy understanding, I worked up this code which gets me closer to a solution but:
filename = "test_file.py"
# open file for writing
my_file = open(filename, "w")
my_file.write("config['ssid'] = 'my_ssid'\n")
my_file.write("config['wifi_pw'] = 'my_pw'\n")
my_file.write("config['mqtt_user'] = 'user'\n")
# open file for reading
my_file = open(filename)
# read the file's entire content
content = my_file.read()
my_file.close()
print('Original file content:')
print(content)
print()
# edit file
new_ssid = 'new_ssid'
new_pw = 'new_pw'
# open file for writing
my_file = open(filename, "w")
text_list = ["config['ssid'] = " + new_ssid + '\n', "config['wifi_pw'] = " + new_pw + '\n']
my_file.writelines(text_list)
# open file for reading
my_file = open(filename)
# read the file's entire content
content = my_file.read()
my_file.close()
print('Edited file content:')
print(content)
Output:
Original file content:
config['ssid'] = 'my_ssid'
config['wifi_pw'] = 'my_pw'
config['mqtt_user'] = 'user'
Edited file content:
config['ssid'] = new_ssid
config['wifi_pw'] = new_pw
it has two errors.
1. It erases the file's content thus showing 2 lines instead of 3 (or more)
2. The new variables' strings are missing quotes.