Python读写配置文件

话不多说,贴代码:

config=ConfigParser.ConfigParser()

strExeDir = get_main_dir()
strFileName = os.path.join(strExeDir, Utf8_To_Gb18030("MQComunicator.config"))
        with open(strFileName) as cfgfile: 
            config.readfp(cfgfile) 
            strQueueMng=config.get('MQ','strQueueMng')
            strChannel=config.get('MQ','strChannel') 
            strConnInfo=config.get('MQ','strConnInfo')
            strSndQName=config.get('MQ','strSndQName')
            strRevQName=config.get('MQ','strRevQName')
            intWaitInterval = config.getint('MQ','intWaitInterval')
            self.WaitSecond = config.getint('MQ','intWaitSecond')

关于get_main_dir():

def main_is_frozen():
    """Return ``True`` if we're running from a frozen program."""
    import imp
    return (
        # new py2exe
        hasattr(sys, "frozen") or
        # tools/freeze
        imp.is_frozen("__main__"))

def get_main_dir():
    """
        获取主程序所在目录的绝对路径
        参数:无
        返回: Gb2312,字符串
    """
    if main_is_frozen():
        return os.path.abspath(os.path.dirname(sys.executable))
    return os.path.abspath(os.path.dirname(sys.argv[0]))

你可能感兴趣的:(python,configparser)