INNER CODE UNIT · Python

remove_empty_dirs

ArcSurge/Termius-Pro-zh_CN · lang.py:135

def remove_empty_dirs(directory):
    """递归删除空目录,返回删除数量"""
    removed_count = 0
    for root, dirs, files in os.walk(directory, topdown=False):
        for dir_name in dirs[:]:
            dir_path = os.path.join(root, dir_name)
            try:
                with os.scandir(dir_path) as it:
                    if not any(it):
                        os.rmdir(dir_path)
                        removed_count += 1
            except OSError as e:
                logging.warning(f"Failed to remove empty directory {dir_path}: {e}")

    if removed_count > 0:
        logging.debug(f"Removed {removed_count} empty directories")
    return removed_count

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…