python读取ini文件

时间:2022-08-25 21:14:55
import configparser
import os
config=configparser.ConfigParser()#创建config对象
file_path=os.path.dirname(os.path.abspath('.'))+'\Python源码\config.ini'#读取文件父目录
config.read(file_path)
sender=config.get('sender','sender')#读取ini配置文件中sender项中的sender值
print(file_path)
print(sender)

  config.ini文件:

[sender]
sender = 666qq.com [receiver]
receiver =8888qq.com

  run结果:

C:\Users\Administrator\Desktop\Python源码\config.ini
666qq.com