#677 ini文件读写   python     snippets     10 months ago (owner) Document
https://blog.51cto.com/hanzhichao/3201113

from configparser import ConfigParser
conf = ConfigParser()
conf.read(configFilePath, encoding=cur_encoding)
print(conf['section']['key'])


''' LutConfig.py '''
from pathlib import Path
from typing import *
import chardet
from configparser import ConfigParser

def GetDefaultConfigFilePath() -> Path:
    return Path(__file__).parent.absolute() / 'Windows/TestLutPro/Config/DefaultGame.ini'

def GetConfig(configFilePath: str) -> dict:
    # 首先二进制方式打开文件
    with open(configFilePath, 'rb') as frb:
        # 检测编码方式
        cur_encoding = chardet.detect(frb.read())['encoding']

    # 指定文件编码方式
    conf = ConfigParser()
    conf.read(configFilePath, encoding=cur_encoding)

    configResult = {}
    section = '/Script/ApplyLutTools.ApplyLutSettings'
    if conf.has_option(section, 'ServerIP'):
        configResult['ServerIP'] = conf[section]['ServerIP']
    if conf.has_option(section, 'ServerPort'):
        configResult['ServerPort'] = conf[section].getint('ServerPort')
    if conf.has_option(section, 'LocalListeningIP'):
        configResult['LocalListeningIP'] = conf[section]['LocalListeningIP']
    return configResult

def GetDefaultConfig() -> dict:
    return GetConfig(GetDefaultConfigFilePath())