import os
import shutil
def compare_and_delete_folders(dir1, dir2, target_dir):
# Get a list of folder names in dir1
folder_names_dir1 = set(next(os.walk(dir1))[1])
# Get a list of folder names in dir2
folder_names_dir2 = set(next(os.walk(dir2))[1])
# Find the common folder names
common_folder_names = folder_names_dir1.intersection(folder_names_dir2)
# Delete the folders with the common names from the target directory
for folder_name in common_folder_names:
folder_path = os.path.join(target_dir, folder_name)
if os.path.exists(folder_path):
shutil.rmtree(folder_path)
print("delete:"+str(folder_path))
print("Folders deleted successfully.")
# Provide the directory paths
dir1 = r'D:\BaiduNetdiskDownload\A'
dir2 = r'D:\BaiduNetdiskDownload\B'
target_dir = r'D:\BaiduNetdiskDownload\A' # Directory to delete the folders from
# Compare the directories and delete the common folders from the target directory
compare_and_delete_folders(dir1, dir2, target_dir)