首先安装环境
创建配置文件和读取文件
config.ini 内容
[email] mail_server=xxxxx mail_username=xxxx mail_password=xxxxx mail_receiver=xxxx
read_config.py内容
import os import configparser #获取文件的当前路径(绝对路径) cur_path=os.path.dirname(os.path.realpath(__file__)) #获取config.ini的路径 config_path=os.path.join(cur_path,'config.ini') conf=configparser.ConfigParser() conf.read(config_path) mail_server=conf.get('email', 'mail_server') mail_username=conf.get('email', 'mail_username') mail_password=conf.get('email', 'mail_password') mail_receiver=conf.get('email', 'mail_receiver')
使用配置文件
from config import read_config print(read_config.mail_server) print(read_config.mail_password)