diff --git a/nomadnet/Directory.py b/nomadnet/Directory.py index dae86ec..f850909 100644 --- a/nomadnet/Directory.py +++ b/nomadnet/Directory.py @@ -97,9 +97,8 @@ class Directory: "announce_stream": self.announce_stream } - file = open(self.app.directorypath, "wb") - file.write(msgpack.packb(directory)) - file.close() + with open(self.app.directorypath, "wb") as file: + file.write(msgpack.packb(directory)) except Exception as e: RNS.log("Could not write directory to disk. Then contained exception was: "+str(e), RNS.LOG_ERROR) @@ -107,10 +106,9 @@ class Directory: def load_from_disk(self): if os.path.isfile(self.app.directorypath): try: - file = open(self.app.directorypath, "rb") - unpacked_directory = msgpack.unpackb(file.read()) + with open(self.app.directorypath, "rb") as file: + unpacked_directory = msgpack.unpackb(file.read()) unpacked_list = unpacked_directory["entry_list"] - file.close() entries = {} for e in unpacked_list: diff --git a/nomadnet/Node.py b/nomadnet/Node.py index 8f03115..acf54ff 100644 --- a/nomadnet/Node.py +++ b/nomadnet/Node.py @@ -128,9 +128,8 @@ class Node: allowed_input = allowed_result.stdout else: - fh = open(allowed_path, "rb") - allowed_input = fh.read() - fh.close() + with open(allowed_path, "rb") as fh: + allowed_input = fh.read() allowed_hash_strs = allowed_input.splitlines() @@ -176,9 +175,8 @@ class Node: generated = subprocess.run([file_path], stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, env=env_map) return generated.stdout else: - fh = open(file_path, "rb") - response_data = fh.read() - fh.close() + with open(file_path, "rb") as fh: + response_data = fh.read() return response_data else: RNS.log("Request denied", RNS.LOG_VERBOSE)