Deepak Deepak - 3 months ago 13
Python Question

Why the value read from properties file in python doesn't work

I am brand new to Python. I have some python script which is accessed by other project to read some data from this python script.

Past:
If suppose I need to provide the version information for the other project from this python script, I was having a class variable which was hard coded and it was working fine till now.

Now:
I wanted to change the way of providing the version information so I choose to read the properties file instead of hard coding. I have added these line in my script

previously it was just this line

class Myclass
version = '1.2'


Now I changed it to

class Myclass
config = ConfigParser.RawConfigParser()
config.read('version.properties')
version = config.get('global', 'version')
print version


my version.properties file looks like

[global]
version= 1.2


when I run this python file to print the version, it is clearly printing in the console. But when this variable is being accessed by that other project the version read from the properties file is not being read (Hard coded value works just fine).

What could be the difference? Why is the value read from properties file is not reflecting?

Answer

If the version.properties file will be in the same directory as your code, you can use this:

import os

config.read(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'version.properties'))
Comments