简体   繁体   中英

Python Configparser not reading file. Says file is not existing

I have project structure is like

Root
  |--config
       |---settings.cfg

  |--utilities
       |---ConfigReader.py

ConfigReader.py

import ConfigParser

config = ConfigParser.ConfigParser()

try:
    with open('./config/settings.cfg') as f:
        config.readfp(f)
except IOError as e:
    raise Exception('Error reading settings.cfg file. '+format(str(e)))

When run above ConfigReader.py, I get always;

    raise Exception('Error reading settings.cfg file. '+format(str(e)))
Exception: Error reading settings.cfg file. [Errno 2] No such file or directory: './config/settings.cfg'

I changed providing filepath with back slash/front slash and dots.None working to me.

What Im doing wrong here?

read like this, also make sure that the file is in path.

config = configparser.ConfigParser()
config.read('./config/settings.cfg')

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