Created
October 28, 2013 13:02
-
-
Save jvrplmlmn/7196441 to your computer and use it in GitHub Desktop.
Examples of the Python core library module "ConfigParser", that helps you creating and interacting with configuration files.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import ConfigParser | |
def createConfig(path): | |
""" | |
Create a config file | |
""" | |
config = ConfigParser.ConfigParser() | |
config.add_section("Settings") | |
config.set("Settings", "font", "Courier") | |
config.set("Settings", "font_size", "10") | |
config.set("Settings", "font_style", "Normal") | |
config.set("Settings", "font_info", | |
"You are using %(font)s at %(font_size)s pt") | |
with open(path, "wb") as config_file: | |
config.write(config_file) | |
def crudConfig(path): | |
""" | |
Create, read, update, delete config | |
""" | |
if not os.path.exists(path): | |
createConfig(path) | |
config = ConfigParser.ConfigParser() | |
config.read(path) | |
# Read some values from the config | |
font = config.get("Settings", "font") | |
font_size = config.get("Settings", "font_size") | |
# Change a value in the config | |
config.set("Settings", "font_size", "12") | |
# Delete a value from the config | |
config.remove_option("Settings", "font_style") | |
# Write changes back to the config file | |
with open(path, "wb") as config_file: | |
config.write(config_file) | |
def interpolationDemo(path): | |
""" | |
Interpolation: using options to build another option | |
""" | |
if not os.path.exists(path): | |
createConfig(path) | |
config = ConfigParser.ConfigParser() | |
config.read(path) | |
print config.get("Settings", "font_info") | |
print config.get("Settings", "font_info", 0, | |
{"font": "Arial", "font_size": "100"}) | |
if __name__ == '__main__': | |
path = "settings.ini" | |
createConfig(path) | |
crudConfig(path) | |
interpolationDemo(path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment