app.ini holds two lines, mode=dark and port=80, each with a new line at its end. A class reads the file in its constructor.
class Config:
def __init__(self, name):
self.data = {}
with open(name) as f:
for line in f:
key, value = line.strip().split("=")
self.data[key] = value
def get(self, key, default="?"):
return self.data.get(key, default)
c = Config("app.ini")
print(c.get("mode"), c.get("port") * 2, c.get("user"))
What does the program print?Log in to join the discussion.
Loading…