在哪里交,团购网站 seo,华大网站建设,仿站下载工具我们有时候不希望将配置参数写在代码里#xff0c;而作为单独的文件传入
一种办法是设置环境变量参数#xff0c;根据这个参数来决定读取哪个配置文件
# _*_ coding: utf-8 _*_
# Time : 2022/7/6 18:03
# Author : Michael
# File : os_environ.py
# desc :
import osdef …我们有时候不希望将配置参数写在代码里而作为单独的文件传入
一种办法是设置环境变量参数根据这个参数来决定读取哪个配置文件
# _*_ coding: utf-8 _*_
# Time : 2022/7/6 18:03
# Author : Michael
# File : os_environ.py
# desc :
import osdef get_env_filename():srv os.environ.get(cnf) # 通过环境变量参数读取相关配置if srv not in [online, sim, qa]:raise Exception(fconfig error: {srv})return f.env_{srv} # 配置文件名字if __name__ __main__:print(get_env_filename())window set varnameval
D:\gitcode\Python_learning\myNoteset cnfonline
D:\gitcode\Python_learning\myNoteecho %cnf%
online
D:\gitcode\Python_learning\myNotepython os_environ.py
.env_onlineD:\gitcode\Python_learning\myNoteset cnfon
D:\gitcode\Python_learning\myNotepython os_environ.py
Traceback (most recent call last):File os_environ.py, line 15, in moduleprint(get_env_filename())File os_environ.py, line 11, in get_env_filenameraise Exception(fconfig error: {srv})
Exception: config error: onlinux export varnameval
(base) /mnt/d/gitcode/Python_learning/myNote$ export cnfonline
(base) /mnt/d/gitcode/Python_learning/myNote$ echo $cnf
online
(base) /mnt/d/gitcode/Python_learning/myNote$ python os_environ.py
.env_online(base) /mnt/d/gitcode/Python_learning/myNote$ export cnfon
(base) /mnt/d/gitcode/Python_learning/myNote$ echo $cnf
on
(base) /mnt/d/gitcode/Python_learning/myNote$ python os_environ.py
Traceback (most recent call last):File os_environ.py, line 15, in moduleprint(get_env_filename())File os_environ.py, line 11, in get_env_filenameraise Exception(fconfig error: {srv})
Exception: config error: on