13

I have created a small Python GUI for controlling the I2C pins of my MCU board. Now I want to try and save the settings of this GUI into a config file, so that the file settings could be changed based on the MCU being used.

I have no idea how to create a config file. I tried to looking into links on how to create and use a config file (e.g. ConfigParse), but could not understand much. Can somebody please help me out?

I am using Python 3.4 on Windows 7.

Ateeb
  • 5,413
  • 3
  • 10
  • 17
Goldengirl
  • 957
  • 4
  • 10
  • 30
  • Does this answer your question? [Python: How would you save a simple settings/config file?](https://stackoverflow.com/questions/19078170/python-how-would-you-save-a-simple-settings-config-file) – Michael Delgado Oct 18 '22 at 04:21

1 Answers1

12

You're on the right tracks with using ConfigParser! Linked are the docs that should be very useful when programming using it.

For you, the most useful think will likely be the examples, which can be found here. A simple program to write a config file can be found below

import configparser
config = configparser.ConfigParser()
config['DEFAULT'] = {'ServerAliveInterval': '45',
                     'Compression': 'yes',
                     'CompressionLevel': '9'}
config['bitbucket.org'] = {}
config['bitbucket.org']['User'] = 'hg'
config['topsecret.server.com'] = {}
topsecret = config['topsecret.server.com']
topsecret['Port'] = '50022'     # mutates the parser
topsecret['ForwardX11'] = 'no'  # same here
config['DEFAULT']['ForwardX11'] = 'yes'
with open('example.ini', 'w') as configfile:
  config.write(configfile)

This program will write some information to the file "example.ini". A program to read this:

import configparser
config = configparser.ConfigParser()
config.read('example.ini')
print(config.sections()) #Prints ['bitbucket.org', 'topsecret.server.com']

Then you can simply use it like you would any other dictionary. Accessing values like:

config['DEFAULT']['Compression'] #Prints 'yes'

Credit given to python docs.

Ateeb
  • 5,413
  • 3
  • 10
  • 17
Alexander Craggs
  • 7,874
  • 4
  • 24
  • 46
  • Thank you for that quicj response Popey Gilbert :).. I was wondering after creating the configparse file do I store the files as .py files? I am sorry if it seems like a silly question.. – Goldengirl Mar 30 '15 at 11:07
  • The program will work for just about every file extension. It is however, better practise to use one of the more common config file extensions such as ".ini" or ".cfg". No matter what extension you use, the file will have the same contents, and the contents is what the config parser reads. – Alexander Craggs Mar 30 '15 at 11:09
  • Hello again.. I tried the same example that you said.. I created a file example.ini to write the config file and a tried to read the file from the python shell by executing the read commands one by one.. But the config.section gives me a error saying ---AttributeError: 'ConfigParser' object has no attribute 'section'.. Could you please tell me what is wrong.. :) – Goldengirl Mar 30 '15 at 11:27
  • Interesting. I mostly just copied and pasted that from the wiki, which is very rarely wrong. Let me go test it quickly. Just checked, http://i.imgur.com/RIE9WbE.png - I did note a mistake on the last line of the config reader, it needs another bracket. – Alexander Craggs Mar 30 '15 at 11:49
  • Sorry for the late response.. I just wanted to thank you for your help.. I was busy with somethin else so could not reply earlier.. Thanks again :) – Goldengirl Apr 10 '15 at 08:09
  • @NamitaRaju Not a problem :) – Alexander Craggs Apr 10 '15 at 12:50