Python读取、配置INI文件

Python读取、配置INI文件


读取及配置ini文件,储存数据参数等信息。

Python代码

import os,re
import configparser

class INI_object:
    def __init__(self):
    	//function:  初始化INI文件       
        config_file_path = self.getProjectPath() + "\\configure.ini"
        self.cf = configparser.ConfigParser()
        self.cf.read(config_file_path)
        
    def getProjectPath(self):
    	//function:  获取当前工程绝对路径  
    	str = os.path.dirname(os.path.abspath(__file__))
    	file_path = str + "\\config.ini"
    	return str
        
    def get(self,section, option):
    	//function:  读取INI文件配置信息.
        try:
    	    ret = self.cf.get(section, option)
    	    return ret
        except:
    	    print("不能获取到%s选项 %s的值." %(section,option)) 
    	    exit(1)
            
	def set(self,section, option, value):
    	//function:   设置INI文件配置信息.         
    	return self.cf.set(section, option, value)
        
if __name__ == "__main__":
    ini = INI_object()
    config = ini.get('Section', 'Option')

猜你喜欢

转载自blog.csdn.net/TsChronic/article/details/83893605