I have a config file FOO in /etc/sysconfig/. This Linux file is very similar to INI-File, but without a section declaration.
In order to retrieve a value from this file, I used to write a shell script like:
source /etc/sysconfig/FOO echo $MY_VALUE
Now I want to do the same thing in python. I tried to use ConfigParser, but ConfigParser does not accept such an INI-File similar format, unless it has a section declaration.
Is there any way to retrieve value from such a file?
Correct initialization sequence for Linux serial port
1:printing uid of a file on linux system
subprocessmodule and reading it's output. How to handle key pressed in a Linux console in C?Use it with the
shelloption set to
True.. simulating atm communication without atm switch
2:Why setupterm terminates the program?
ConfigParser, you could did any thing like:. Python - detect keyboard layout
The code creates an in-memory filelike object containing a [main] section header and the contents of the specified file. ConfigParser then reads this filelike object..
#! /usr/bin/env python2.6 from StringIO import StringIO import ConfigParser def read_configfile_without_sectiondeclaration(filename): buffer = StringIO() buffer.write("[main]\n") buffer.write(open(filename).read()) buffer.seek(0) config = ConfigParser.ConfigParser() config.readfp(buffer) return config if __name__ == "__main__": import sys config = read_configfile_without_sectiondeclaration(sys.argv) print config.items("main")