提交 fdcd4b44 编写于 作者: K Kentaro Wada

Use with open to avoid leaving unclosed files

上级 6b65fa37
...@@ -29,7 +29,8 @@ def update_dict(target_dict, new_dict, validate_item=None): ...@@ -29,7 +29,8 @@ def update_dict(target_dict, new_dict, validate_item=None):
def get_default_config(): def get_default_config():
config_file = osp.join(here, 'default_config.yaml') config_file = osp.join(here, 'default_config.yaml')
config = yaml.load(open(config_file)) with open(config_file) as f:
config = yaml.load(f)
return config return config
...@@ -54,13 +55,14 @@ def get_config(config_from_args=None, config_file=None): ...@@ -54,13 +55,14 @@ def get_config(config_from_args=None, config_file=None):
save_config_file = True save_config_file = True
if os.path.exists(config_file): if os.path.exists(config_file):
user_config = yaml.load(open(config_file)) or {} with open(config_file) as f:
user_config = yaml.load(f) or {}
update_dict(config, user_config, validate_item=validate_config_item) update_dict(config, user_config, validate_item=validate_config_item)
if save_config_file: if save_config_file:
try: try:
yaml.safe_dump(config, open(config_file, 'w'), with open(config_file, 'w') as f:
default_flow_style=False) yaml.safe_dump(config, f, default_flow_style=False)
except Exception: except Exception:
logger.warn('Failed to save config: {}'.format(config_file)) logger.warn('Failed to save config: {}'.format(config_file))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册