from gi.repository import GLib config = GLib.KeyFile() config.load_from_file("config.ini", GLib.KeyFileFlags.NONE) # Get value of a key username = config.get_string("database", "username") password = config.get_string("database", "password") # Get a list of keys within a section keys = config.get_keys("database")
from gi.repository import GLib config = GLib.KeyFile() config.set_string("database", "username", "john") config.set_string("database", "password", "password123") # Write to file with open("config.ini", "w") as f: f.write(config.to_data())In this example, we create a new instance of the KeyFile object and set some key-value pairs. Once we have set everything we need, we can write the contents of the KeyFile object to a file. The package/library used in these code examples is gi.repository GLib KeyFile, which is part of the GObject Introspection bindings for Python. This package contains a set of Python bindings for the GLib library, which is written in C and provides low-level facilities for working with strings, arrays, trees, and other data types.