`

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");
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics