Only close files when they've been opened

This commit is contained in:
Oskar Winkels 2021-11-11 19:33:45 +01:00
parent 27f8b6d5d0
commit bfb7f477af
Signed by: o.winkels
GPG Key ID: E7484A06E99DAEF1
1 changed files with 2 additions and 4 deletions

View File

@ -19,10 +19,9 @@ def startup():
try: try:
fd = open(filename) fd = open(filename)
data = json.load(fd) data = json.load(fd)
fd.close()
except FileNotFoundError: except FileNotFoundError:
print("DB file not found, creating on POST/write.") print("DB file not found, creating on POST/write.")
finally:
fd.close()
print("Read", len(data), "quizzes from DB") print("Read", len(data), "quizzes from DB")
@ -54,10 +53,9 @@ def post():
try: try:
fd = open(filename, 'w+') fd = open(filename, 'w+')
json.dump(data, fd, indent=2) json.dump(data, fd, indent=2)
fd.close()
except e: except e:
print("Failed to open/create DB file for writing:", e) print("Failed to open/create DB file for writing:", e)
finally:
fd.close()
return id, 201 return id, 201