python模拟windows获取设置ini

#!/usr/bin/env python
from ConfigParser import ConfigParser;

def GetIniString(strFile, strSection, strKey):
    strNullRet = ""

    if (not os.path.exists(strFile)) or (not strSection) or (not strKey):
        return strNullRet

    cfg = ConfigParser()
    try:
        cfg.read(strFile)

        if not cfg.has_section(strSection):
            return strNullRet

        strVal = cfg.get(strSection, strKey)
    except:
        return strNullRet

    return strVal


def SetIniString(strFile, strSection, strKey, strVal):
    bRet = False;
    
    if (not strFile) or (not strSection) or (not strKey) or (not strVal):
        return False;
    cfg = ConfigParser();
    try:
        cfg.read(strFile);
        if not cfg.has_section(strSection):
            cfg.add_section(strSection);
        cfg.set(strSection, strKey, strVal);
        cfg.write(open(strFile, 'w'));
        bRet = True;
    except:
        return False;
    return bRet

SetIniString("./a.txt", "a", "b", "c");
 

你可能感兴趣的:(C++,c,windows,python,OS)