gpt4 book ai didi

python - 自动执行目录差异,同时忽略文件中的某些特定行

转载 作者:太空宇宙 更新时间:2023-11-03 19:27:34 25 4
gpt4 key购买 nike

我需要比较两个目录,并生成某种差异的结构化输出(文本文件即可)。也就是说,输出可能如下所示:

file1 exists only in directory2
file2 exists only in directory1
file3 is different between directory1 and directory2

我不在乎格式,只要有信息就行。第二个要求是我需要能够在比较两个文件时忽略某些字符序列。 Araxis Merge 具有此功能:您可以输入正则表达式,并且任何唯一区别在于与该正则表达式匹配的字符序列的文件将被报告为相同。

这将使 Araxis Merge 成为一个很好的候选者,但是,到目前为止我还没有找到方法来生成差异的结构化输出。即使使用命令行参数启动 consolecompare.exe,它也只是打开一个显示差异的 Araxis GUI 窗口。

那么,以下任一情况是否存在?

  • 如何让 Araxis Merge 将差异结果打印到文本文件?
  • 另一个在忽略某些字符的情况下进行比较的实用程序序列,并产生结构化输出?

如果此类实用程序作为 Python 的模块或插件存在,则额外加分。请记住,这必须完全通过命令行/Python 脚本完成 - 没有 GUI。

最佳答案

在某种程度上,普通的旧 diff 命令就可以做到这一点,即比较目录内容并忽略与特定正则表达式模式匹配的更改(使用 -I 选项)。

来自man bash :

-I regexp
Ignore changes that just insert or delete lines that match regexp.

快速演示:

[me@home]$ diff images/ images2
Only in images2: x
Only in images/: y
diff images/z images2/z
1c1
< zzz
---
> zzzyy2

[me@home]$ # a less verbose version
[me@home]$ diff -q images/ images2
Only in images2: x
Only in images/: y
Files images/z and images2/z differ

[me@home]$ # ignore diffs on lines that contain "zzz"
[me@home]$ diff -q -I ".*zzz.*" images/ images2/
Only in images2/: x
Only in images/: y

关于python - 自动执行目录差异,同时忽略文件中的某些特定行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7517850/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com