通过visual studio创建的python项目在添加ini配置文件后的读取方式
ini配置文件名随意修改
需要导入configparser包
config.ini
[system] # 组名
HOST=10.241.203.70
# lpd服务端口
LPDPORT=515
# raw服务端口
RAWPORT=9100
# 打印文件保存文件夹
SAVE_FOLDER=D:/spool/
DEBUG=True
PrintServer.py
import sys
import configparser
#######################
HOST = '127.0.0.1'
# lpd服务端口
LPDPORT = 515
RAWPORT = 9100
# lpd服务接收的数据保存路径
SAVE_FOLDER = 'C:/'
#######################
DEBUG = False
try:
config = configparser.ConfigParser()
config.read("./config.ini")
# 获取属性值
HOST = config.get('system','HOST')
if(HOST is None or HOST == ''):
HOST = '127.0.0.1'
# lpd服务端口
LPDPORT = config.get('system','LPDPORT')
if(LPDPORT is not None or LPDPORT != ''):
LPDPORT = int(LPDPORT)
# raw服务端口
RAWPORT = config.get('system','RAWPORT')
if(RAWPORT is not None or RAWPORT != ''):
RAWPORT = int(RAWPORT)
SAVE_FOLDER = config.get('system','SAVE_FOLDER')
DEBUG = (config.get('system','DEBUG') == 'True')
except Exception as e:
loginfo.error('初始化服务错误:' + str(e))
sys.exit(0)
if __name__ == "__main__":
# 运行主函数
这个只是代码片段