简体   繁体   English

来自文件的Python ConfigParser

[英]Python ConfigParser from file

I try to load config from file with read 我尝试从读取文件中加载配置

But when it reads from same folder it works 但是,当它从同一文件夹读取时,它可以工作

When I read from parent it doesn't. 当我从父母那里读到的时候没有。

Code: 码:

/
config.ini
Conf/
    __init__.py
    config.ini
    Conf.py

// Conf.py // Conf.py

config = ConfigParser()

#this works
config.read(‘config.ini’)

#this doesn’t 
config.read(‘./../config.ini’)

Any ideas? 有任何想法吗?

Thanks! 谢谢!

Just tried the same thing here, and both work: 只是在这里尝试了同样的事情,并且都可以工作:

import configparser

config = configparser.ConfigParser()

#this works
print(config.read('config.ini'))

#this works too
print(config.read('./../config.ini'))

This solved my question in the end: 这最终解决了我的问题:

import configparser
import os.path

conf = configparser.ConfigParser()

conf.read(os.path.dirname(__file__)+'/../config.ini')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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