From bfb7f477af480200ac842f0cf9a7b7ed172cf266 Mon Sep 17 00:00:00 2001 From: Oskar Date: Thu, 11 Nov 2021 19:33:45 +0100 Subject: [PATCH] Only close files when they've been opened --- fs-quiz-tool-db/app.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/fs-quiz-tool-db/app.py b/fs-quiz-tool-db/app.py index 8cee0ce..5e51341 100644 --- a/fs-quiz-tool-db/app.py +++ b/fs-quiz-tool-db/app.py @@ -19,10 +19,9 @@ def startup(): try: fd = open(filename) data = json.load(fd) + fd.close() except FileNotFoundError: print("DB file not found, creating on POST/write.") - finally: - fd.close() print("Read", len(data), "quizzes from DB") @@ -54,10 +53,9 @@ def post(): try: fd = open(filename, 'w+') json.dump(data, fd, indent=2) + fd.close() except e: print("Failed to open/create DB file for writing:", e) - finally: - fd.close() return id, 201