From d98ec61c8b24c9e1171da13489cee7feae60d255 Mon Sep 17 00:00:00 2001 From: ego-lay-atman-bay <70973578+ego-lay-atman-bay@users.noreply.github.com> Date: Thu, 11 Jul 2024 10:36:48 -0600 Subject: [PATCH] make sure log folder exists before trying to list it --- src/main.py | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/src/main.py b/src/main.py index d0c2f8a..a4d1afd 100644 --- a/src/main.py +++ b/src/main.py @@ -74,28 +74,29 @@ def setup_logger( debug: bool = False, ): log_filename = os.path.join(dir, f'{datetime.now().strftime(name)}.{extension}') - - log_files = os.listdir(dir) - logs = [] - createLogger('file', filename = log_filename, debug = debug) - for file in log_files: - if file == os.path.basename(log_filename): - continue + if os.path.isdir(dir): + log_files = os.listdir(dir) + logs = [] + - try: - logs.append((datetime.strptime(os.path.splitext(file)[0], name), file)) - except ValueError: - continue - - logs.sort(key = lambda i: i[0]) - - logs = logs[max(0, keep-1)::] - - for log in logs: - logging.debug(f'deleting log: {log[1]}') - os.remove(os.path.join(dir, log[1])) + for file in log_files: + if file == os.path.basename(log_filename): + continue + + try: + logs.append((datetime.strptime(os.path.splitext(file)[0], name), file)) + except ValueError: + continue + + logs.sort(key = lambda i: i[0]) + + logs = logs[max(0, keep-1)::] + + for log in logs: + logging.debug(f'deleting log: {log[1]}') + os.remove(os.path.join(dir, log[1])) return log_filename