"""Utility functions related to operations with files.""" import os import shutil from sentry_sdk import capture_exception def remove_file(path): """Remove a file, and silently return if it doesn't exist. Args: path (str): path to file """ try: os.remove(path) except FileNotFoundError: pass def clean_directory(path): """Remove all files and sub directories from provided directory. Args: path (str): path to directory """ try: if os.path.exists(path): for file_object in os.listdir(path): object_path = os.path.join(path, file_object) if os.path.isfile(object_path): os.unlink(object_path) else: shutil.rmtree(object_path) else: os.makedirs(path) except Exception as e: capture_exception(e)