40media How to retrieve value from etc/sysconfig in Python


How to retrieve value from etc/sysconfig in Python



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
I suppose you could do exactly what you're doing with your shell script using the subprocess module and reading it's output.


How to handle key pressed in a Linux console in C?
Use it with the shell option set to True..
simulating atm communication without atm switch


Can I run original MS.Net Framework 4 on Linux someway (with Wine perhaps)?


Garbage data from serial port

2:



Why setupterm terminates the program?
If you want to use ConfigParser, you could do something like:.
Python - detect keyboard layout
#! /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[1])     print config.items("main") 
The code creates an in-memory filelike object containing a [main] section header and the contents of the specified file.

ConfigParser then reads that filelike object..



50 out of 100 based on 25 user ratings 400 reviews