600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > python config文件的读写

python config文件的读写

时间:2024-02-05 04:10:34

相关推荐

python config文件的读写

1、设置配置文件

[mysql]host = 1234port = 3306user = rootpassword = Zhsy08241128database = leartd

2、读取配置文件

import configparserimport osconf= configparser.ConfigParser()def readConf():'''读取配置文件'''root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))conf.read(root_path + '/ceshi/conf/app.conf') # 文件路径print(conf)name = conf.get("mysql", "host") # 获取指定section 的option值print(name)

3、写入配置文件

def writeConf():'''写入配置文件'''root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))conf.read(root_path + '/ceshi/conf/app.conf') # 文件路径conf.set("mysql", "host", "1234") # 修改指定section 的optionconf.write(open(root_path + '/ceshi/conf/app.conf', 'w'))

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。