terminal output improvements + flush=True for print()

This commit is contained in:
2024-02-23 21:23:23 +00:00
parent 08281194c2
commit bf7aaa12f2
4 changed files with 24 additions and 23 deletions

View File

@@ -11,9 +11,9 @@ def mark_file_as_BAD(file: str, bad_exception: Exception) -> None:
os.makedirs(bad_dir, exist_ok=True)
bad_file_path = os.path.join(bad_dir, filename)
shutil.move(file, bad_file_path)
print(f'\n[Warning] Found BAD compressed file: {filename}\nMoved to: {bad_file_path}\nError message: {bad_exception}')
print(f'\n[Warning] Found BAD compressed file: {filename}\nMoved to: {bad_file_path}\nError message: {bad_exception}\n', flush=True)
except Exception as e:
print(f'\n[Error] {e}')
print(f'\n[ERROR] {e}\n', flush=True)
def extract_zip(zip_file: str, target_dir: str) -> None | Exception:
try:
@@ -24,7 +24,7 @@ def extract_zip(zip_file: str, target_dir: str) -> None | Exception:
except zipfile.BadZipfile as e:
mark_file_as_BAD(zip_file, e)
except Exception as e:
print(f'\n[ERROR] Something went wrong while extracting the contents of a submitted zip file. Check the error message, get student id and download / organise manually\nError message: {e}')
print(f'\n[ERROR] Something went wrong while extracting the contents of a submitted zip file. Check the error message, get student id and download / organise manually\n\nError message: {e}\n', flush=True)
return e
def extract_rar(rar_file: str, target_dir: str) -> None:
@@ -45,7 +45,7 @@ def extract_rar(rar_file: str, target_dir: str) -> None:
except rarfile.NotRarFile as e:
mark_file_as_BAD(rar_file, e)
except rarfile.RarCannotExec as e:
print('\n[Error] Missing unrar tool\nfor Windows: make sure file UnRAR.exe exists in directory \'utils\'\nfor Linux/Mac: need to install unrar (check README)')
print('\n[ERROR] Missing unrar tool\nfor Windows: make sure file UnRAR.exe exists in directory \'utils\'\nfor Linux/Mac: need to install unrar (check README)\n', flush=True)
exit()
def extract_7z(seven_zip_file: str, target_dir: str) -> None:
@@ -73,4 +73,4 @@ def extract_file_to_dir(file_path: str, student_dir: str) -> None | Exception:
elif file_path.lower().endswith('.7z'):
extract_7z(file_path, student_dir)
else:
print(f"\n[Error] unknown file type: {file_path}")
print(f"\n[ERROR] unknown file type: {file_path}\n", flush=True)