Method for python selenium to automatically read configuration files

import configparser
import os
import platform

if platform.system()=='windows': #判断操作系统是否为windows系统
    #__file__获取当前这个文件的文件名
    #os.path.abspath(__file__)获取当前文件的绝对路径,路径包含文件名
    #os.path.dirname(os.path.abspath(__file__))获取当前文件的绝对路径,不包含文件名
    #然后拼接上db.ini文件名,即可获得db.ini的绝对路径
    configFilePath=os.path.dirname(os.path.abspath(__file__))+'\db.ini'
else:
    #mac电脑的斜线是反的
    configFilePath=os.path.dirname(os.path.abspath(__file__))+'/db.ini'

print(configFilePath)
cf=configFilePath.ConfigParser() #根据db.ini路径生成配置文件对象
cf.read(configFilePath) #读取db.ini配置文件
cf.sections() #读取配置文件的section部分
cf.options('gloryroad') #读取gloryroad section下的所有配置项
dbname=cf.get('gloryroad','dbname') #读取dbname的值
username=cf.get('gloryroad','username')
password=cf.get('gloryroad','password')

Guess you like

Origin blog.csdn.net/weixin_44123630/article/details/113839686