python read .cfg file code example

Example 1: python config file

''' config.cfg
[whatever]
key=qwerertyertywert2345
secret=sadfgwertgrtujdfgh
'''

from configparser import ConfigParser

config = ConfigParser()
config.read('config.cfg')

my_key = config['whatever']['key']
my_secret = config['whatever']['secret']

Example 2: how to use config file to remove redundant variables in code

from ConfigParser import SafeConfigParser

parser = SafeConfigParser()
parser.read('simple.ini')

print parser.get('bug_tracker', 'url')

Tags:

Misc Example