Python讀取、配置INI檔案
阿新 • • 發佈:2018-11-09
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')