gpt4 book ai didi

linux - 如何以 pythanic 方式加载所有配置文件?

转载 作者:太空宇宙 更新时间:2023-11-04 12:02:08 25 4
gpt4 key购买 nike

只是想知道是否有任何正确的方法可以将多个配置文件加载到 python 脚本中。目录结构如下。

 dau
|-APPS
|---kafka
|---brokers
|-ENVS

根据上面的内容,我的基本目录是 dau。我打算将脚本保存在 Kafka 和 Broker 目录中。所有全局环境都以“.ini”格式存储在 ENVS 目录中。我想将这些 ini 文件加载到所有脚本中,而不是一个一个地添加,因为我们将来可能需要添加更多的环境文件,在这种情况下,我们不必在每个脚本上手动添加它们。

示例 env.ini

[DEV]
SERVER_NAME = dev123.abcd.net

我试图使用下面链接的答案,但我们仍然必须手动添加它们,或者如果 dau 目录中的父路径发生变化,我们必须编辑代码。

Stack-flow-answer

最佳答案

您好,我提出了以下解决方案,感谢您的支持。

下面的代码将获取所有.ini 文件作为列表并返回。

import os

def All_env_files():
try:
BASE_PATH = os.path.abspath(os.path.join(__file__,"../.."))

ENV_INI_FILES = [os.path.join(BASE_PATH + '/ENVS/',each) for each in os.listdir(BASE_PATH + '/ENVS') if each.endswith('.ini')]

return ENV_INI_FILES
except ValueError:
raise ValueError('Issue with Gathering Files from ENVS Directory')

下面的代码将获取列表 ini 文件并将其提供给 ConfigParser。

import ConfigParser, sys , os
"""
This is for kafka broker status check
"""
#Get Base path
Base_PATH = os.path.abspath(os.path.join(__file__,"../../.."))
sys.path.insert(0, Base_PATH)
#Importing configs python file on ../Configs.py
import Configs, edpCMD

#Taking All the ENVS ini file as list
List_ENVS = Configs.All_env_files()

请随意提供任何更短的方法。

关于linux - 如何以 pythanic 方式加载所有配置文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51982290/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com