Python Forum
Updating a config file [solved]
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Updating a config file [solved]
#5
(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.
Reply


Messages In This Thread
Updating a config file [solved] - by ebolisa - Nov-03-2021, 11:49 PM
RE: Updating a config file - by bowlofred - Nov-04-2021, 12:30 AM
RE: Updating a config file - by ebolisa - Nov-04-2021, 12:35 AM
RE: Updating a config file - by bowlofred - Nov-04-2021, 12:53 AM
RE: Updating a config file - by ebolisa - Nov-04-2021, 08:13 AM
RE: Updating a config file - by Gribouillis - Nov-04-2021, 09:18 AM
RE: Updating a config file - by ebolisa - Nov-04-2021, 09:31 AM
RE: Updating a config file - by ebolisa - Nov-04-2021, 09:44 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python openyxl not updating Excel file MrBean12 1 370 Mar-03-2024, 12:16 AM
Last Post: MrBean12
Question [SOLVED] Correct way to convert file from cp-1252 to utf-8? Winfried 8 1,035 Feb-29-2024, 12:30 AM
Last Post: Winfried
  Updating sharepoint excel file odd results cubangt 1 892 Nov-03-2023, 05:13 PM
Last Post: noisefloor
  Loop through json file and reset values [SOLVED] AlphaInc 2 2,194 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  [Solved by deanhystad] Create a zip file using zipfile library DZ_Galaxy 2 1,200 Aug-17-2022, 04:57 PM
Last Post: DZ_Galaxy
  Initializing, reading and updating a large JSON file medatib531 0 1,810 Mar-10-2022, 07:58 PM
Last Post: medatib531
  |SOLVED] Glob JPGs, read EXIF, update file timestamp? Winfried 5 2,526 Oct-21-2021, 03:29 AM
Last Post: buran
  [SOLVED] Read text file from some point till EOF? Winfried 1 1,987 Oct-10-2021, 10:29 PM
Last Post: Winfried
  [SOLVED] Input parameter: Single file or glob? Winfried 0 1,602 Sep-10-2021, 11:54 AM
Last Post: Winfried
Thumbs Up [SOLVED] Find last occurence of pattern in text file? Winfried 4 4,461 Aug-13-2021, 08:21 PM
Last Post: Winfried

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020