我今天才知道,python还有这么神奇的库。自带的,可以直接读配置文件。
下面是我做的一个小例子。
[red]
file_path = red.txt
file_size = 12
file_name = red
file_ch_name = 红色
create_time = 20190628
[blue]
file_path = blue.txt
file_size = 10
file_name = blue
file_ch_name = 蓝色
create_time = 20190629
[green]
file_path = green.txt
file_size = 3
file_name = green
file_ch_name = 绿色
create_time = 20190630
# _*_ coding:utf-8 _*_
from configparser import ConfigParser
# 读取配置
def get_config(config_file_path="config.conf"):
config = ConfigParser()
config.read(config_file_path, encoding='utf-8')
return config
config_file_path = "config.conf"
conf = get_config(config_file_path)
ch_name = conf.get("blue", "file_ch_name")
print(ch_name)
运行结果