0
点赞
收藏
分享

微信扫一扫

Pytest学习-读取yaml和ini代码封装

elvinyang 2022-08-07 阅读 135

一、读取yaml和读取ini代码优化

思路:两个读取文件放到一个读取文件

废话不罗嗦,直接上代码

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project :Pytest
@File :read_data.py
@IDE :PyCharm
@Author :zhou
@Date :2022/8/6 19:05
"""
import configparser
import os

import yaml

# 网址的URL
yaml_path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "config", "data.yaml")

# 获取settings_ini文件里面的URL
ini_path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "config", "settings_ini")


class FileRead:
def __init__(self):
self.data_path = yaml_path
self.ini_path = ini_path

def read_data(self):
# 获取文件
f = open(self.data_path, encoding="utf-8")
# 读取文件内容
data = yaml.safe_load(f)
return data

def read_ini(self):
config = configparser.ConfigParser()
config.read(self.ini_path, encoding="utf-8")
return config


base_data = FileRead()

Pytest学习-读取yaml和ini代码封装_Pytest

举报

相关推荐

0 条评论