import os
import configparser
current_path =os.path.dirname(__file__)#獲取config當前文件路徑
config_file_path = os.path.join(current_path,'..','conf','localconfig.ini')#獲取配置文件的路徑
class ConfigUtils: #類封裝、駝峯式命名法
def __init__(self,cfg_path=config_file_path):
self.cfg =configparser.ConfigParser()#創建一個配置文件對象
self.cfg.read(cfg_path) #創建好後去讀取cfg_path
def HOSTS(self):
hosts_value = self.cfg.get('default','hosts') #獲取節和key
return hosts_value
local_config = ConfigUtils() #創建對象,測試代碼
if __name__=='__main__':
print(local_config.HOSTS())