From b8394471ecea8515a93ec07fe9533d794e1e9c57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Behmo?= Date: Mon, 15 Mar 2021 23:26:38 +0100 Subject: [PATCH] feat: catch errors when writing a file where a directory exists This error sometimes happens when developing new plugins. --- tutor/utils.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tutor/utils.py b/tutor/utils.py index a7cb7cb..aa81ab4 100644 --- a/tutor/utils.py +++ b/tutor/utils.py @@ -43,6 +43,18 @@ def ensure_file_directory_exists(path: str) -> None: Create file's base directory if it does not exist. """ directory = os.path.dirname(path) + if os.path.isfile(directory): + raise exceptions.TutorError( + "Attempting to create a directory, but a file with the same name already exists: {}".format( + directory + ) + ) + if os.path.isdir(path): + raise exceptions.TutorError( + "Attempting to write to a file, but a directory with the same name already exists: {}".format( + directory + ) + ) if not os.path.exists(directory): os.makedirs(directory)