版权声明:QQ群:796245415 个人技术交流,禁止用作商业活动 https://blog.csdn.net/chen498858336/article/details/83795877
# coding=utf-8
我的配置文件名叫e:config.ini
[mysqlconf]
host=127.0.0.1
port=3306
#1521
user=Ywx606554
password=133365
db_name=see_db
代码如下:
# coding = utf-8
import os
import ConfigParser
config_dir = os.path.split(os.path.realpath(__file__))[0]
config_path = os.path.join(config_dir, "config.ini")
def get_config_value(section, option):
config = ConfigParser.SafeConfigParser()
config.read(config_path)
value = config.get(section=section, option=option)
return value
def has_config():
config = ConfigParser.SafeConfigParser()
config.read(config_path)
print config.has_option("mysqlconf", "port")
print config.has_section("mysqlconf")
print config.options("mysqlconf")
config.set("mysqlconf","host","192.168.110.2")
print config.items("mysqlconf")
# config.remove_option('mysqlconf', 'host')
config.add_section('new_sect')
print config.sections()
def allow_no_value():
config = ConfigParser.SafeConfigParser(allow_no_value=True)
config.read(config_path)
print config.get("mysqlconf","name")
# print get_config_value("mysqlconf", "db_name")
# has_config()
allow_no_value()