From 40f4770e177e4003f12758b9f75c3373e56562c2 Mon Sep 17 00:00:00 2001 From: flifloo Date: Tue, 7 Jun 2022 10:44:52 +0200 Subject: [PATCH] Fix links.json creation --- twitfix.py | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/twitfix.py b/twitfix.py index 78a1edc..61fd57e 100644 --- a/twitfix.py +++ b/twitfix.py @@ -81,17 +81,14 @@ if link_cache_system == "json": json.dump(default_link_cache, outfile, indent=4, sort_keys=True) try: - f = open('links.json',) - link_cache = json.load(f) - except json.decoder.JSONDecodeError: + with open('links.json', "r") as f: + link_cache = json.load(f) + except (json.decoder.JSONDecodeError, FileNotFoundError): print(" ➤ [ X ] Failed to load cache JSON file. Creating new file.") - link_cache = {} - except FileNotFoundError: - print(" ➤ [ X ] Failed to load cache JSON file. Creating new file.") - link_cache = {} - finally: - f.close() - + with open('links.json', "w") as f: + link_cache = {} + json.dump(link_cache, f) + elif link_cache_system == "db": client = pymongo.MongoClient(config['config']['database'], connect=False) table = config['config']['table']