python的ConfigParser模块 读取配置文件内容

uwsgi.ini 

[uwsgi]
processes=4
# 工作进程中的线程数目
threads=2
# 指定主进程
master= True
# 记录主进程的进程id
pidfile=wsgi.pid
# uwsgi日志文件
daemonize=uwsgi.log
[ceshi]
DD = dfd

去取文件内容

# -*- coding: utf-8 -*-
# @Time    : 2019/3/6 9:17
from configparser import ConfigParser
from pathlib import Path,WindowsPath,PosixPath
# todo Path 已经集成了 WindowsPath,PosixPath
path= Path(__file__)
print(path)  # D:\kfsd\readini.py
print(path.cwd())  # D:\kfsd
print(path.parent)  # D:\kfsd
print(path.cwd().parent.parent)  # D:\
path = path.parent

# 拼接ini 路径
ini_path = path.joinpath('uwsgi.ini') # 客户放入多个目录
print(path.joinpath('ini','uw.py'))  # D:\kfsd\ini\uw.py
print(ini_path)  # D:\kfsd\uwsgi.ini

cf = ConfigParser()

cf.read(ini_path,encoding='utf-8')

print(cf.sections())  # ['uwsgi', 'ceshi']
# todo 会把大写转成小写
print(cf.options('ceshi'))   # ['dd']
# print(cf.get('uwsgi','ff'))  # 没有会报错
cc = cf['uwsgi']
master = cc.get("master",'')  # 没有不会报错 返回默认值
print(master)  # True
print(type(master))  # <class 'str'>

猜你喜欢

转载自blog.csdn.net/weixin_37989267/article/details/88220213