Tuesday, April 22, 2014
Python - read configuration file
Consider the following configuration file which consists of section name and key values:
$ cat /opt/myapp/myapp.ini
[master]
host=host01
port=2181
The following code (myapp.py) will parse the configuration file extracting values by section + corresponding key:
#!/usr/bin/python
import ConfigParser
configParser = ConfigParser.RawConfigParser()
configFilePath = r'/opt/myapp/myapp.ini'
configParser.read(configFilePath)
myhost = configParser.get('master', 'host')
myport = configParser.get('master', 'port')
print "Checking host:"+myhost+",port:"+myport
Run:
$ ./myapp.py
checking host:host01,port:2181
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment