From b7b702baef973810859ab9f08c84648573d84e90 Mon Sep 17 00:00:00 2001 From: josiah Date: Tue, 28 Jan 2020 04:44:08 +0000 Subject: [PATCH] Move to "with file open" syntax from "file.read()" style. --- arke.py | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/arke.py b/arke.py index f2ce39f..762ea9d 100644 --- a/arke.py +++ b/arke.py @@ -90,24 +90,27 @@ while is_on: json.dump(datastore, outfile, ensure_ascii=False, sort_keys=True) # track state - file = open(this_round_file, "r") - if os.path.exists(last_round_file): - stateFile = open(last_round_file, "r") - else: - stateFile = open(last_round_file, "w+") - - oldData = stateFile.read() - if oldData != file.read(): - stateChanged = True - else: - stateChanged = False + with open(this_round_file, "a+", encoding="utf-8") as file: + if os.path.exists(last_round_file): + stateFile = open(last_round_file, "r") + else: + stateFile = open(last_round_file, "w+") + + oldData = stateFile.read() + if oldData != file.read(): + stateChanged = True + else: + stateChanged = False + + stateFile.close() # delete state.log so I can write to it cleanly os.remove(last_round_file) # queue up an alert if stateChanged = True results = [] - with open(this_round_file, "r") as json_File: + with open(this_round_file, "r+", encoding="utf-8") as json_File: + pdb.set_trace() json_data = json.load(json_File) for item in json_data: results.append(item) @@ -119,10 +122,9 @@ while is_on: errorFile.write(errorText) # Copy current results to state.log file for next iteration - errorFile = open(last_round_file, "a+") - errorFile.write(json_string) - errorFile.write(cert_json) - errorFile.write(domain_json) - errorFile.close() + with open(last_round_file, "a+") as json_File: + json_datastore = json.dumps(datastore, ensure_ascii=False, sort_keys=True) + json_File.write(json_datastore) + os.remove(this_round_file) time.sleep(60)