andylin02 发表于 2013-1-26 14:24:53

python模拟windows获取设置ini

#!/usr/bin/env pythonfrom 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 strValdef 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 bRetSetIniString("./a.txt", "a", "b", "c");
页: [1]
查看完整版本: python模拟windows获取设置ini