lama/utilities/data_clean_up.py
def rm_by_name(root: Path, name: str, to_keep: List):
    """
    Remove directories. If any path or part path is in to keep, delete the rest of the folder put keep that one.
    """
    dirs = root.glob(f'**/{name}') # Get all matching directories

    for d in dirs:

        subfolders_to_keep = []

        if not d.is_dir():
            continue

        for subdir in d.iterdir():
            if not subdir.is_dir():
                continue

            for subseq in to_keep:

                if is_subseq(Path(subseq).parts, subdir.parts):
                    subfolders_to_keep.append(subdir)

        if not to_keep:
            # Theres no subfolders to keep, delete the whole directory
            shutil.rmtree(d)
        elif not subfolders_to_keep and to_keep:
            # There is a folder we should be keeping, but it's not present. May a typo?
            # Just in cases, do not delete
            raise ValueError(f'Could not find specified subfolder to keep {to_keep} in {d}')
        else:
            # We have located the subdirs to keep. Now delte the rest of the folder
            for subdir in d.iterdir():
                if not subdir.is_dir():
                    continue
                if subdir not in subfolders_to_keep:
                    shutil.rmtree(subdir)

    Remove directories. If any path or part path is in to keep, delete the rest of the folder put keep that one.
    
Remove directories. If any path or part path is in to keep, delete the rest of the folder put keep that one.
Remove directories . If any path or part path is in to keep delete the rest of the folder put keep that one .
def rm_by_name(root: Path, name: str, to_keep: List):
    dirs = root.glob(f'**/{name}') 
    for d in dirs:
        subfolders_to_keep = []
        if not d.is_dir():
            continue
        for subdir in d.iterdir():
            if not subdir.is_dir():
                continue
            for subseq in to_keep:
                if is_subseq(Path(subseq).parts, subdir.parts):
                    subfolders_to_keep.append(subdir)
        if not to_keep:
            shutil.rmtree(d)
        elif not subfolders_to_keep and to_keep:
            raise ValueError(f'Could not find specified subfolder to keep {to_keep} in {d}')
        else:
            for subdir in d.iterdir():
                if not subdir.is_dir():
                    continue
                if subdir not in subfolders_to_keep:
                    shutil.rmtree(subdir)
def rm_by_name ( root : Path , name : str , to_keep : List ) : dirs = root . glob ( f'**/{name}' ) for d in dirs : subfolders_to_keep = [ ] if not d . is_dir ( ) : continue for subdir in d . iterdir ( ) : if not subdir . is_dir ( ) : continue for subseq in to_keep : if is_subseq ( Path ( subseq ) . parts , subdir . parts ) : subfolders_to_keep . append ( subdir ) if not to_keep : shutil . rmtree ( d ) elif not subfolders_to_keep and to_keep : raise ValueError ( f'Could not find specified subfolder to keep {to_keep} in {d}' ) else : for subdir in d . iterdir ( ) : if not subdir . is_dir ( ) : continue if subdir not in subfolders_to_keep : shutil . rmtree ( subdir )
Remove directories.
Remove directories .
"""
    Remove directories. If any path or part path is in to keep, delete the rest of the folder put keep that one.
    """ # Get all matching directories # Theres no subfolders to keep, delete the whole directory # There is a folder we should be keeping, but it's not present. May a typo? # Just in cases, do not delete # We have located the subdirs to keep. Now delte the rest of the folder
root
Path
name
str
to_keep
List