删除两个路径下的同名文件夹

该Python脚本使用os和shutil模块比较两个目录`dir1`和`dir2`中的文件夹名称,找出共同的文件夹名,然后从目标目录`target_dir`中删除这些同名文件夹。它首先获取每个目录的文件夹列表,找到交集,然后遍历并删除目标目录中的对应文件夹。
摘要由CSDN通过智能技术生成

删除两个路径下的同名文件夹

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)

可以使用Java编写自定义代码并将其添加到Katalon Studio项目中,然后在测试用例或测试套件中调用该代码。以下是一个示例代码,可以比较两个文件夹同名文件之间的差异: ```java import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.util.List; public class FolderComparator { public static void compareFolders(String folderPath1, String folderPath2) throws IOException { File folder1 = new File(folderPath1); File folder2 = new File(folderPath2); if (!folder1.isDirectory() || !folder2.isDirectory()) { throw new IllegalArgumentException("Both parameters must be valid directories!"); } File[] files1 = folder1.listFiles(); File[] files2 = folder2.listFiles(); if (files1.length != files2.length) { System.out.println("The two folders have different number of files!"); return; } for (int i = 0; i < files1.length; i++) { File file1 = files1[i]; File file2 = new File(folder2.getAbsolutePath() + "/" + file1.getName()); if (!file2.exists()) { System.out.println("File " + file1.getName() + " not found in " + folderPath2); continue; } List<String> lines1 = Files.readAllLines(file1.toPath()); List<String> lines2 = Files.readAllLines(file2.toPath()); if (!lines1.equals(lines2)) { System.out.println("File " + file1.getName() + " is different in the two folders!"); } } System.out.println("Folder comparison complete!"); } } ``` 这个代码接受两个文件夹路径作为参数,并比较它们下面同名文件之间的差异。如果发现某个文件在一个文件夹中存在而在另一个文件夹中不存在,或者两个文件内容不一致,它会输出相应的消息。你可以在测试用例中调用这个代码,如下所示: ```java FolderComparator.compareFolders("path/to/folder1", "path/to/folder2"); ``` 其中,"path/to/folder1"和"path/to/folder2"分别是你要比较的两个文件夹路径。如果你想在测试套件中使用这个代码,你可以将它放在一个Test Listener中,并在测试套件中添加这个Test Listener,这样每次运行测试套件时都会执行这个代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值