使用 with 语句写入文件

This commit is contained in:
YuCheng Hu 2021-03-11 10:49:27 -05:00
parent ff5bc31174
commit 8800a0f19b
No known key found for this signature in database
GPG Key ID: 1E5CBEF8B550FB7D
1 changed files with 3 additions and 3 deletions

View File

@ -2,6 +2,7 @@
# Author - HoneyMoose (https://www.ossez.com) # Author - HoneyMoose (https://www.ossez.com)
import json import json
import ruamel.yaml as yaml import ruamel.yaml as yaml
json_filename = 'resources/black_rock_test.json' json_filename = 'resources/black_rock_test.json'
@ -18,6 +19,5 @@ with open(json_filename) as json_file:
json_file.close() json_file.close()
# Write to YAML # Write to YAML
yaml_file = open(yaml_filename, 'w+') with open(yaml_filename, 'w') as yaml_file:
yaml.dump(data, yaml_file, allow_unicode=True) yaml.dump(data, yaml_file, allow_unicode=True)
yaml_file.close()