yaml文件管理,包括读取yaml文件内容,写入yaml文件,清空yaml文件。
import pytest
import yaml
# 读取yaml文件
def read_yaml(file):
with open(file, 'r', encoding='utf-8') as f:
content = f.read()
data = yaml.load(content, yaml.FullLoader)
return data
# 追加写入yaml文件
def write_yaml(file, data):
with open(file, 'a', encoding='utf-8') as f:
yaml.dump(data, f)
# 清除yaml文件内容
def clean_yaml(file):
with open(file, 'r+', encoding='utf-8') as f:
f.seek(0) # 把文件定位到第一个位置
f.truncate() # 清空文件
print('执行测试前,先清空yaml文件内容~')
if __name__ == '__main__':
# read_yaml('key.yml')
key = {
"exponent": "123456",
"modulus": "12345678901234567890-"
}
write_yaml('key.yml', key)
# clean_yaml('key.yml')