cornsnake.util_robust_delete
Functions for recursively deleting directories and their contents.
1""" 2Functions for recursively deleting directories and their contents. 3 4[Documentation](http://docs.mrseanryan.cornsnake.s3-website-eu-west-1.amazonaws.com/cornsnake/util_robust_delete.html) 5""" 6 7import os 8import shutil 9import stat 10 11 12def _rmrf(temp_dir): 13 """ 14 Recursively delete a directory and its contents. 15 16 - Give this user read, write, execute permissions (so we can navigate and delete) but without affecting other users. 17 18 Args: 19 temp_dir (str): The path to the directory to be deleted. 20 """ 21 st = os.stat(temp_dir) 22 os.chmod(temp_dir, st.st_mode | stat.S_IXUSR | stat.S_IRUSR | stat.S_IWUSR) 23 shutil.rmtree(temp_dir) 24 25 26def _delete_files_recursively(temp_dir): 27 """ 28 Recursively delete files in a directory. 29 30 Args: 31 temp_dir (str): The path to the directory containing files to be deleted. 32 """ 33 for dirpath, _dirnames, filenames in os.walk(temp_dir): 34 for f in filenames: 35 fp = os.path.join(dirpath, f) 36 try: 37 os.remove(fp) 38 except FileNotFoundError: 39 pass # Intentionally NOT passing exception onwards 40 except PermissionError: 41 pass # Intentionally NOT passing exception onwards 42 43 44def _delete_dirs_recursively(temp_dir): 45 """ 46 Recursively delete directories in a directory. 47 48 Args: 49 temp_dir (str): The path to the directory containing directories to be deleted. 50 """ 51 for dirpath, dirnames, _filenames in os.walk(temp_dir): 52 for d in dirnames: 53 dp = os.path.join(dirpath, d) 54 try: 55 _rmrf(dp) 56 except FileNotFoundError: 57 pass # Intentionally NOT passing exception onwards 58 except PermissionError: 59 pass # Intentionally NOT passing exception onwards 60 61 62def _delete_dir_contents(temp_dir): 63 """ 64 Delete files and directories in a directory. 65 66 Args: 67 temp_dir (str): The path to the directory containing files and directories to be deleted. 68 """ 69 _delete_files_recursively(temp_dir) 70 _delete_dirs_recursively(temp_dir) 71 72 73def delete_dirs(temp_dirs_to_delete): 74 """ 75 Delete directories and their contents. 76 - Python holds on to some paths - so delete as much as we can 77 78 Args: 79 temp_dirs_to_delete (list): A list of directory paths to be deleted. 80 """ 81 dirs_with_locked_items = [] 82 # faster delete: 83 for temp_dir in temp_dirs_to_delete: 84 try: 85 _rmrf(temp_dir) 86 except FileNotFoundError: 87 dirs_with_locked_items.append(temp_dir) 88 # Intentionally NOT passing exception onwards 89 except PermissionError: 90 dirs_with_locked_items.append(temp_dir) 91 # Intentionally NOT passing exception onwards 92 93 # slower, more robust delete: 94 for temp_dir in dirs_with_locked_items: 95 _delete_dir_contents(temp_dir)
def
delete_dirs(temp_dirs_to_delete):
74def delete_dirs(temp_dirs_to_delete): 75 """ 76 Delete directories and their contents. 77 - Python holds on to some paths - so delete as much as we can 78 79 Args: 80 temp_dirs_to_delete (list): A list of directory paths to be deleted. 81 """ 82 dirs_with_locked_items = [] 83 # faster delete: 84 for temp_dir in temp_dirs_to_delete: 85 try: 86 _rmrf(temp_dir) 87 except FileNotFoundError: 88 dirs_with_locked_items.append(temp_dir) 89 # Intentionally NOT passing exception onwards 90 except PermissionError: 91 dirs_with_locked_items.append(temp_dir) 92 # Intentionally NOT passing exception onwards 93 94 # slower, more robust delete: 95 for temp_dir in dirs_with_locked_items: 96 _delete_dir_contents(temp_dir)
Delete directories and their contents.
- Python holds on to some paths - so delete as much as we can
Args: temp_dirs_to_delete (list): A list of directory paths to be deleted.