From 2043b92cd445614f2996e8b40b675039da28578e Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Sat, 31 Jul 2021 20:07:41 +0200 Subject: [PATCH] more checks in move function --- utility/file_management.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/utility/file_management.py b/utility/file_management.py index 84d70bd..8437af2 100644 --- a/utility/file_management.py +++ b/utility/file_management.py @@ -15,10 +15,19 @@ def copy_file(filename: str, destination: str = "", delete_existing_file: bool = def move_file(file_name: str, destination: str = ""): - if os.path.exists(file_name): - try: - shutil.copy2(file_name, destination) - os.remove(file_name) - except FileNotFoundError as error: - print("File not found!") - print(error) + if not os.path.exists(file_name): + print(f'move_file: File {file_name} does not exist') + return + if not os.path.exists(destination): + print(f'move_file: Destination directory {destination} does not exist') + return + try: + shutil.copy2(file_name, destination) + os.remove(file_name) + return + except FileNotFoundError as error: + print(error) + print('move_file: File not found') + except shutil.SameFileError as error: + print(error) + print('move_file: Files are the same!')