简体   繁体   中英

using a ConfigParser file in python program

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 cset : not found

What am i doing wrong?

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

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM