Loading settings.
★★☆Many applications read and write settings into an initialisation (ini) file. Typically the data in the file is structured into blocks identified with square brackets. Each setting has a name, followed by an equal sign, followed by the data. E.g. in the example below, port is a setting and the data is 143. ``` [modified by] name=Dave Hillyard [database] ; use IP address in case network name resolution is not working server=192.0.2.62 port=143 ```
Write a program that reads the contents of a text file as shown above and outputs the data contained within it in the format:
Last modified by: Dave Hillyard
IP address: 192.0.2.62
Port number: 143
Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.
input_settings that:settings.ini. If the file is not found the message, "settings.ini file not found." is output and no further processing takes place. Note the file is included in the Trinket above for you to use as source data.Note the data attributes can be anywhere in the file, not necessarily in the order presented above.
input_settings function is called.File contents for settings.ini:
[modified by]
name=Dave Hillyard
[database]
; use IP address in case network name resolution is not working
server=192.0.2.62
port=143
Output:
Last modified by: Dave Hillyard
IP address: 192.0.2.62
Port number: 143
Restricted automated feedback
Automated feedback for this assignment is still under construction. Submitted programs are checked for syntax errors and their source code is checked for potential errors, bugs, stylistic issues, and suspicious constructs. However, no checks are performed yet to see if the program correctly implements the behaviour specified in the assignment.
# ini file program
# -------------------------
# Subprograms
# -------------------------
---
def input_settings():
---
# Check file exists
try:
---
file = open("settings.ini", "r")
---
# No file found
except FileNotFoundError:
---
print("settings.ini file not found.")
---
# File found
else:
---
# Process each line in the file
for line in file:
---
line = line.strip()
---
# Split data into a list, separated by an equal sign
data = line.split("=")
---
# If there are two items in the list, match the first item
if len(data) > 1:
---
# Set the variable to be the second item
match data[0]:
---
case "name":
---
modified = data[1]
---
case "server":
---
ip = data[1]
---
case "port":
---
port = data[1]
---
file.close()
---
print("Last modified by:", modified)
print("IP address:", ip)
print("Port number:", port)
---
# -------------------------
# Main program
# -------------------------
input_settings()