From 9cb3c8a81be3650d87e44dc943fd7ba47a1be6b0 Mon Sep 17 00:00:00 2001 From: Marcel Stimberg Date: Fri, 28 Jul 2023 14:48:41 +0200 Subject: [PATCH] open/close files with context manager --- brian2genn/device.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/brian2genn/device.py b/brian2genn/device.py index fa3b472..a3d62c2 100644 --- a/brian2genn/device.py +++ b/brian2genn/device.py @@ -1013,8 +1013,8 @@ def run(self, directory, use_GPU, with_output): check_call(["./main", "test", str(self.run_duration)], cwd=directory) self.has_been_run = True - last_run_info = open( - os.path.join(directory, 'results/last_run_info.txt')).read() + with open(os.path.join(directory, 'results/last_run_info.txt')) as f: + last_run_info = f.read() self._last_run_time, self._last_run_completed_fraction = map(float, last_run_info.split()) @@ -1821,8 +1821,8 @@ def generate_makefile(self, directory, use_GPU): if os.sys.platform == 'win32': project_tmp = GeNNCodeObject.templater.project_vcxproj(None, None, source_files=self.source_files) - open(os.path.join(directory, 'project.vcxproj'), 'w').write( - project_tmp) + with open(os.path.join(directory, 'project.vcxproj'), 'w') as f: + f.write(project_tmp) else: compile_args_gcc = get_gcc_compile_args() linker_flags = ' '.join(prefs.codegen.cpp.extra_link_args) @@ -1830,7 +1830,8 @@ def generate_makefile(self, directory, use_GPU): source_files=self.source_files, compiler_flags=compile_args_gcc, linker_flags=linker_flags) - open(os.path.join(directory, 'Makefile'), 'w').write(makefile_tmp) + with open(os.path.join(directory, 'Makefile'), 'w') as f: + f.write(makefile_tmp) def generate_objects_source(self, arange_arrays, net, static_array_specs, synapses, writer):