6
0
mirror of https://github.com/ChristianLight/tutor.git synced 2024-12-13 14:43:03 +00:00

feat: catch errors when writing a file where a directory exists

This error sometimes happens when developing new plugins.
This commit is contained in:
Régis Behmo 2021-03-15 23:26:38 +01:00
parent 0a670d7ead
commit b8394471ec

View File

@ -43,6 +43,18 @@ def ensure_file_directory_exists(path: str) -> None:
Create file's base directory if it does not exist. Create file's base directory if it does not exist.
""" """
directory = os.path.dirname(path) 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): if not os.path.exists(directory):
os.makedirs(directory) os.makedirs(directory)