当前位置: 移动技术网 > IT编程>脚本编程>Python > Python中读取config配置文件的方法

Python中读取config配置文件的方法

2020年07月07日  | 移动技术网IT编程  | 我要评论
import configparser
import os

class read_config:
    """读取配置文件的类"""
    def __init__(self, file_path=None):
        if file_path:
            config_path = file_path
        else:
            root_dir = os.path.dirname(os.path.abspath('./source'))
            config_path = os.path.join(root_dir, "config.ini")
            if not os.path.exists(config_path):
                os.system('taskkill /f /im %s' % 'python.exe')
        self.cf = configparser.RawConfigParser()
        self.cf.read(config_path, encoding='utf-8')

    # 读取配置文件中值的方法
    def get_content(self, param1, param2):
        value = self.cf.get(param1, param2)
        return value

配置文件:

[FILE]
PATH=C:\Users\xxxx\Desktop
NAME=aaa.xls

获取方法:

rc = read_config()
PATH = rc.get_content('FILE', 'PATH')

本文地址:https://blog.csdn.net/M_Power2/article/details/107164634

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网