----------------------文件后缀名 .ini---------------------- [log] current_module_name=当前模块名 total_level=INFO file_name=钱诚贷日志.log
--------------------------------------------------------------------------------------------
----------------------------------封装调用ini-----------------------------------------
import os from configparser import ConfigParser from Explanation.heand_path import cfg_dir #cfg_dir是ini放置在目录的位置 class HandleConfig(ConfigParser): def __init__(self,file_path_name): super().__init__() self.read(file_path_name,encoding='utf-8') file = os.path.join(cfg_dir, 'config.ini') cfg = HandleConfig(file) if __name__ == '__main__': a = cfg.get('log','file_name') print(a)
---------------------------------------------------------------------------------------
=================================单条使用========================================
#读取 #conf = ConfigParser() #实例化一个类 #conf.read('Yang_ini.ini',encoding='utf-8') #读取配置文件 #value = conf.get('log','name') #读取某项配置的值字符串格式 #s = conf.sections('log') #获取当前的section下的所有option #修改 #conf.set('log','file_name','py303030.log') #写入到内存当中 #conf.write(open('Yang_ini.ini','w',encoding ='utf-8'))