[FIX] File object not closed for ``dataset_description.json``
Fixes
@@ -107,7 +107,8 @@ class BIDSLayout(Layout):
"project root. Every valid BIDS dataset must have "
"this file.")
else:
- self.description = json.load(open(target, 'r'))
+ with open(target, 'r') as desc_fd:
+ self.description = json.load(desc_fd)
for k in ['Name', 'BIDSVersion']:
if k not in self.description:
raise ValueError("Mandatory '%s' field missing from "
@@ -183,7 +184,8 @@ class BIDSLayout(Layout):
for deriv in deriv_dirs:
dd = os.path.join(deriv, 'dataset_description.json')
- description = json.load(open(dd, 'r'))
+ with open(dd, 'r') as ddfd:
+ description = json.load(ddfd)
pipeline_name = description.get('PipelineDescription.Name', None)
if pipeline_name is None:
raise ValueError("Every valid BIDS-derivatives dataset must "
@@ -587,8 +589,8 @@ class MetadataIndex(object):
for json_file_path in reversed(potential_jsons):
if os.path.exists(json_file_path):
- param_dict = json.load(open(json_file_path, "r",
- encoding='utf-8'))
+ with open(json_file_path, 'r', encoding='utf-8') as fd:
+ param_dict = json.load(fd)
results.update(param_dict)
return results
