首页 > 解决方案 > 指示在 Python 的 D:/database.ini 文件中找不到部分 postgresql 的消息

问题描述

我想使用 Python 连接到我的数据库。这是我写的函数:

def config(filename='D:/database.ini', section='postgresql'):
    # create a parser
    parser = ConfigParser()
    # read config file
    parser.read(filename)
    # get section, default to postgresql
    db = {}
    if parser.has_section(section):
        params = parser.items(section)
        for param in params:
            db[param[0]] = param[1]
    else:
        raise Exception('section {0} not found in the {1} file'.format(section, filename))
    return db

这是 database.ini 文件的内容:

[postgresql]
host=localhost
database=corpus
user=postgres
password=1234

但是,会出现以下消息:

section postgresql not found in the D:/database.ini file

我应该怎么办?

标签: pythonpostgresqlini

解决方案


推荐阅读