Created
October 26, 2009 00:55
-
-
Save goozbach/218348 to your computer and use it in GitHub Desktop.
python-configparser
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
# this is a comment | |
[default] | |
foo = yes | |
bar = no | |
cam = maybe | |
var = 23 | |
prefix = /var/lib/ | |
dir = %(prefix)s/bar/ |
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
#!/usr/bin/python | |
import ConfigParser | |
import os | |
import sys | |
config = ConfigParser.ConfigParser() | |
_execdir = os.path.dirname(sys.argv[0]) | |
_configfile = os.path.join(_execdir,'foo.ini') | |
config.read(_configfile) | |
print _configfile | |
# print out a var from config | |
print config.get('default', 'foo', 0) # -> "Python is fun!" | |
# set the configs into global vars | |
for foo in config.items('default'): | |
item, value = foo | |
globals()[item] = value | |
print "globals" | |
# dir should come from the config file | |
print dir |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment