Amin Bassam | Web Geek 📎

Developing solutions for your digital needs

import os

def delete_empty_folders(path):
    for folder_name in os.listdir(path):
        folder_path = os.path.join(path, folder_name)
        if os.path.isdir(folder_path):
            if not os.listdir(folder_path):
                print("Deleting folder:", folder_path)
                os.rmdir(folder_path)
                continue
            delete_empty_folders(folder_path)

# Replace "C:\\test" with the path of the directory you want to search
directory_path = "C:\\test"

# Call the delete_empty_folders function to delete all empty folders in the directory
delete_empty_folders(directory_path)
import os

def list_empty_folders(path):
    empty_folders = []
    for folder_name in os.listdir(path):
        folder_path = os.path.join(path, folder_name)
        if os.path.isdir(folder_path):
            if not os.listdir(folder_path):
                empty_folders.append(folder_path)
            else:
                empty_folders.extend(list_empty_folders(folder_path))
    return empty_folders

# Replace "C:\\test" with the path of the directory you want to search
directory_path = "C:\\test"

# Call the list_empty_folders function to get a list of all empty folders in the directory
empty_folders = list_empty_folders(directory_path)

# Print the list of empty folders
print("Empty folders in", directory_path, ":")
for folder in empty_folders:
    print(folder)

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *