python使用自带的configparser模块用来读取配置文件,配置文件的形式类似windows中的ini文件

目录

​1、新建配置文件​

​2、读取配置文件​

​3、打印读取内容​


1、新建配置文件

  config.ini

[config]
#ID
id = 3739980950
#限制文本长度
textLength = 0
#限制图片数量
imgCount = 0
# 文件目录
dir = .\\wb\\

2、读取配置文件

# -*- coding: utf-8 -*-
# 导入依赖
import configparser

# 创建配置类对象
cf = configparser.ConfigParser()

# 读取配置文件
cf.read("config.ini", encoding="utf-8")

# 获取文件中的所有配置
sections = cf.sections()
print(sections)

# 获取某个section名为config所对应的键
options = cf.options("config")
print(options)

# 获取config配置中的id值
id = cf.get("config", "id")
print(id)

3、打印读取内容

['config']
['id', 'textlength', 'imgcount', 'dir']
3739980950