0

How to read config.properties placed in separate package/directory using python 2.7 ?

import ConfigParser
config = ConfigParser.RawConfigParser()
config.read('ConfigFile.properties')

print config.get('default', 'test.name')

This of course works if config file is in same package, but if placed in different package - what is the best practice to read the file ?

1 Answer 1

2
  1. variable __file__ contains path to current python module file;
  2. os.path.dirname(os.path.abspath(__file__)) yields parent directory name for python file;
  3. if data file located inside python package, use function pkgutil.get_data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.