ini file python code example

Example 1: python gzip file

import gzip
f_in = open('/home/joe/file.txt')
f_out = gzip.open('/home/joe/file.txt.gz', 'wb')
f_out.writelines(f_in)
f_out.close()
f_in.close()

Example 2: 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 3: 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')