Read and write config files in python

import configparser
config = configparser.ConfigParser()

Example conf file:

[DEFAULT]
sendcalendarreminders = False
writeexcelfile = True

[location]
billdir = /home/joel/Documents/Bills
excelfilename = creditcarddues.xlsx

[cards]
prefix_icici = ICICI_
password_icici = mypass

Reading conf file:

config.read(os.path.expanduser('~/getcreditcards.conf'))
SendCalendarReminders = config['DEFAULT']['sendcalendarreminders']
WriteExcelFile = config['DEFAULT']['writeexcelfile']
billdir=config['location']['billdir']
prefix_icici = config['cards']['prefix_icici']

A gotcha you’ve got to remember is that when you read the conf file, everything you get is a string. There are no integers, or booleans. If you want any other data type, you’ve got to convert specifically to that.