Search code examples
pythonpropertiesconfigparser

How to read $xyz value from .properties file in python?


I have a build.properties file like

[directory]
src = src
srcdir = ${src}/fix
srcext = ${srcdir}/extensions
srct = ${srcext}/xyz

my aim is to get the full value of srct i mean it is to be src/fix/extensions/xyz

Is there any way of getting the value of cde? I tried configparser and jproperties but i couldn't get the required output.


Solution

  • You can use ExtendedInterpolation class which implements more advanced syntax.

    >>> from configparser import ConfigParser, ExtendedInterpolation
    >>> 
    >>> parser = ConfigParser(interpolation=ExtendedInterpolation())
    >>> parser.read("build.properties")
    ['build.properties']
    >>> print(parser["directory"]["srct"])
    src/fix/extensions/xyz