cfg.ini 如下所示
[email_qq]
sender = 321@qq.com
psw = 123456
port = 789
smtp_server = trf.qq.com
[email_163]
sender = 123@qq.com
psw = 123456
port = 789
smtp_server = yre.qq.com
读取cfg.ini的脚本
import configparser
import os
# 获取的__file__所在脚本的路径,也就是fileName.py的路径
curpath = os.path.dirname(os.path.realpath(__file__))
print(curpath)
cfgpath = os.path.join(curpath, "cfg.ini")
print(cfgpath) # cfg.ini的路径
# 创建管理对象
conf = configparser.ConfigParser()
# 读ini文件
conf.read(cfgpath, encoding="utf-8") # python3
# conf.read(cfgpath) # python2
# 获取所有的section
sections = conf.sections()
print(sections) # 返回list
items = conf.items('email_163')
print(items) # list里面对象是元组
宏定义:用于ini配置文件中拼接脚本
CMD_GET_META = "{} -v quiet -print_format json -show_format -show_streams {}"
file_cmd = CMD_GET_META.format('ffmpeg', '001.mp3')
print(file_cmd)
### 结果如下:
ffmpeg -v quiet -print_format json -show_format -show_streams 001.mp3