读取配置文件

# -*- coding:utf-8 -*-
import os
import configparser
import codecs
import json

proDir = os.path.split(os.path.realpath(__file__))[0]
configPath = os.path.join(proDir, "config.ini")
class ReadConfig:
def __init__(self):
fd = open(configPath)
data = fd.read()
# remove BOM
if data[:3] == codecs.BOM_UTF8:
data = data[3:]
file = codecs.open(configPath, "w")
file.write(data)
file.close()
fd.close()
self.cf = configparser.ConfigParser()
self.cf.read(configPath)

def get_http(self,name):
value = self.cf.get('HTTP',name)
return value

def get_database(self,name):
value = self.cf.get('DATABASE',name)
return value

if __name__ == '__main__':
x = ReadConfig()
y = x.get_cookie()
#y = x.get_http('weburl')
print(y)

posted on 2018-03-26 14:17  Overtimer-加班哥  阅读(205)  评论(0)    收藏  举报

导航