1

I am trying to store the settings of my python program(that is used to configure the input and output pins of MCP23017)... I know that there is something called the ConfigParser module I could use to achieve this.. But I don't really understand what the 'example.ini' refers to in this example(first one in the link)? Is it the file that stores the settings? If yes then where is the program whose settings have to be stored being reffered to here?

Update:: I created a config file .. But the File has a variable as follows that contains the i2cset.. x= ('i2cset', '-y', '0', '0x14', '0x20', '0xFF') But when I try to read it in my main program it gives me a error saying Error: /bin/sh: 1: i 2 c s e t : not found

What am i doing wrong?

Any suggestion is welcome, Thank you in advance, Kind Regards, Namita.

2 Answers 2

2

example.ini is a file which can be located anywhere in the directory tree, provided you have read access to it. Of course, the shortest path would be to place it in the same directory as the Python program whose settings it contains.

Following the example on the Python documentation site:

>>> import configparser
>>> config = configparser.ConfigParser()
>>> config['DEFAULT'] = {'ServerAliveInterval': '45',
...                      'Compression': 'yes',
...                      'CompressionLevel': '9'}

>>> with open('example.ini', 'w') as configfile:
...   config.write(configfile)
...

will produce a file example.ini with the following contents:

[DEFAULT]
ServerAliveInterval: 45
Compression: yes
CompressionLevel: 9

It is possible to type the above configuration into a plaintext file yourself, of course.

Then reading the file can be done by a Python program that imports ConfigParser and adapts the example found further down the page.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for that response... How do I create this Config file in case my python program involves functions within them? Do i make the whole function into a section in the config file?
The config file should not store any code, just some variables you use as default settings.
0

Is it ['example.ini'] the file that stores the settings?

Yes.

where is the program whose settings have to be stored being reffered to here?

Any program can read example.init, parse it, and use the settings.

2 Comments

Thank you for that quick response.. So should this configuration file be created as a seperate program by itself or should it be a part of my original python program(whose settings need to be stored)?
A config file is a config file, it is not a program. You will have two files: the config file and the Python code that reads it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.