【发布时间】:2016-09-09 09:19:34
【问题描述】:
(使用 Python 3.4.3)
我想在我的配置文件中使用环境变量,我读到我应该使用SafeConfigParser 和os.environ 作为参数来实现它。
[test]
mytest = %(HOME)s/.config/my_folder
由于我需要获取一个部分中的所有选项,因此我正在执行以下代码:
userConfig = SafeConfigParser(os.environ)
userConfig.read(mainConfigFile)
for section in userConfig.sections():
for item in userConfig.options(section):
print( "### " + section + " -> " + item)
我的结果不是我所期望的。正如您在下面看到的,它不仅获得了我在我的部分 ([test]\mytest) 中的选项,还获得了所有环境变量:
### test -> mytest
### test -> path
### test -> lc_time
### test -> xdg_runtime_dir
### test -> vte_version
### test -> gnome_keyring_control
### test -> user
我做错了什么?
我希望能够将[test]\mytest 解析为/home/myuser/.config/my_folder,但不希望SafeConfigParser 将我的所有环境变量添加到其每个部分。
【问题讨论】:
标签: python environment-variables python-3.4 configparser