简体   繁体   中英

configparser: Not reading the file from the given path

Below is my python project structure in eclipse. I am using configparser to read a section in settings.config file. The configparser is not parsing the file. It shows No Section specified but I have correctly mentioned the section. I am creating a dockerfile and when I run docker-compose file, the path is shown as

/usr/local/lib/python3.6/site-packages/MyProject-0.0.1-py3.6.egg/Publisher/settings.config

MyProject
 -Client
    -__init__.py
    -Main.py
 -Publisher
    -domain1
    -domain2
        -__init__.py
        -config.py
        -callconfig.py
    -__init__.py
    -settings.config

#config.py
import os.path as path
try:
    from configparser import ConfigParser
except ImportError:
    from ConfigParser import ConfigParser  # ver. < 3.0
        class Config(object):

        def get(self, mykey):
            config = ConfigParser()   
            configpath = path.join(path.split(path.dirname(path.abspath(__file__)))[0], "settings.config") 
            print (configpath)
            config.read(configpath)
            myvalue = config.get('mysection', mykey) 

Just specify the parent folder path or just go one hierarchy up

  configpath = 'Publisher/settings.config'
    config.read(configpath)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM