import shutil import logging from pathlib import Path logging.basicConfig( level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s", ) logger: logging.Logger = logging.getLogger(__name__) def sort_files(folder: str) -> None: working_folder: Path = Path(folder) if not working_folder.exists(): logger.critical("Folder isn't exist. Exiting...") raise FileExistsError("Folder isn't exist") logger.info("Target folder found. Continue...") logger.info("Creating child folders...") Path.mkdir(working_folder / "images", exist_ok=True) Path.mkdir(working_folder / "documents", exist_ok=True) Path.mkdir(working_folder / "archives", exist_ok=True) Path.mkdir(working_folder / "other", exist_ok=True) logger.info("Child folders created...") logger.info("Starting search and sorting...") for file in working_folder.glob("*.*"): if file.suffix in [".gif", ".png", ".jpg"]: logger.info( f"Found {file.absolute()}. Moving to {working_folder.absolute()}/images" ) shutil.move(file.absolute(), f"{working_folder.absolute()}/images") continue if file.suffix in [".pdf", ".docx", ".txt"]: logger.info( f"Found {file.absolute()}. Moving to {working_folder.absolute()}/documents" ) shutil.move(file.absolute(), f"{working_folder.absolute()}/documents") continue if file.suffix in [".zip", ".tar", ".gz"]: logger.info( f"Found {file.absolute()}. Moving to {working_folder.absolute()}/archives" ) shutil.move(file.absolute(), f"{working_folder.absolute()}/archives") continue logger.info( f"Found {file.absolute()}. Moving to {working_folder.absolute()}/other" ) shutil.move(file.absolute(), f"{working_folder.absolute()}/other") logger.info("Sorting completed")