yaml文件管理,包括讀取yaml文件內(nèi)容蒲拉,寫入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文件內(nèi)容
def clean_yaml(file):
with open(file, 'r+', encoding='utf-8') as f:
f.seek(0) # 把文件定位到第一個位置
f.truncate() # 清空文件
print('執(zhí)行測試前芬沉,先清空yaml文件內(nèi)容~')
if __name__ == '__main__':
# read_yaml('key.yml')
key = {
"exponent": "123456",
"modulus": "12345678901234567890-"
}
write_yaml('key.yml', key)
# clean_yaml('key.yml')