Most Pythonic way to store (small) configuration

Tim Chase python.list at tim.thechases.com
Wed Aug 5 09:18:52 EDT 2015


On 2015-08-02 12:11, Cecil Westerhof wrote:
> There are a lot of ways to store configuration information:
> - conf file
> - xml file
> - database
> - json file
> - and possible a lot of other ways
> 
> I want to write a Python program to display cleaned log files. I do
> not think I need a lot of configuration to be stored:
> - some things relating to the GUI
> - default behaviour
> - default directory
> - log files to display, including some info
>   - At least until where it was displayed
> 
> Because of this I think a human readable file would be best.

Yet another mostly-built-in option is to just have a simple file of
key/value pairs, optionally with comments.  This can be read with
something like

  config = {}
  with open('config.ini') as f:
    for row in f:
      row = row.strip()
      if not row or row.startswith(('#', ';')):
        continue
      k, _, v = row.partition('=')
      config[k.strip().upper()] = v.lstrip()

which is pretty straight-forward and easy format to edit.

-tkc





More information about the Python-list mailing list