通过使用 Python 排除隐藏文件对两个目录执行差异
Perform diff on two directories by excluding hidden files using Python
我正在尝试编写一个 Python 脚本,在两个目录上执行 diff -r
。我想排除目录中的隐藏文件。
这是我的。
source = "/Users/abc/1/"
target = "/Users/abc/2/"
bashCommand = 'diff -x ".*" -r ' + source + ' ' + target
# Below does not work either
# bashCommand = "diff -x '.*' -r " + source + ' ' + target
import subprocess
process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
output, error = process.communicate()
if output:
print "Directories do not match: " + output
else:
print "Directories match"
我知道我应该使用 -x '.*'
来忽略点文件。我看了 this 所以 post。但这没有帮助。我应该如何写这一行?
bashCommand = 'diff -x ".*" -r ' + source + ' ' + target
编辑 1:
这个我也试过了,也没用
pat = "-x \".*\""
bashCommand = "diff " + pat + " -r " + source + " " + target
print bashCommand
当我手动打印输出和 运行 命令时,它按预期工作。但是 Python 脚本没有产生预期的结果
$python BashCommand.py
diff -x ".*" -r /Users/abc/1/ /Users/abc/2/
Directories do not match: Only in /Users/abc/1/: .a
$diff -x ".*" -r /Users/abc/1/ /Users/abc/2/
$
在bash中,单引号和双引号的含义不同。来自 Difference between single and double quotes in Bash
Enclosing characters in single quotes (') preserves the literal value of each character within the quotes.
而对于双引号:
The special parameters * and @ have special meaning when in double quotes (see Shell Parameter Expansion).
因此您的 ".*"
在通过 diff
之前正在扩展。尝试切换引号
bashCommand = "diff -x '.*' -r " + source + ' ' + target
编辑
Popen 通常不使用 shell 来执行你的命令(除非你传递 shell=True
)所以你实际上根本不需要转义模式:
>>> subprocess.Popen(['diff', '-x', "'.*'", '-r', 'a', 'b'])
<subprocess.Popen object at 0x10c53cb50>
>>> Only in a: .dot
>>> subprocess.Popen(['diff', '-x', '.*', '-r', 'a', 'b'])
<subprocess.Popen object at 0x10c53cb10>
我正在尝试编写一个 Python 脚本,在两个目录上执行 diff -r
。我想排除目录中的隐藏文件。
这是我的。
source = "/Users/abc/1/"
target = "/Users/abc/2/"
bashCommand = 'diff -x ".*" -r ' + source + ' ' + target
# Below does not work either
# bashCommand = "diff -x '.*' -r " + source + ' ' + target
import subprocess
process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
output, error = process.communicate()
if output:
print "Directories do not match: " + output
else:
print "Directories match"
我知道我应该使用 -x '.*'
来忽略点文件。我看了 this 所以 post。但这没有帮助。我应该如何写这一行?
bashCommand = 'diff -x ".*" -r ' + source + ' ' + target
编辑 1: 这个我也试过了,也没用
pat = "-x \".*\""
bashCommand = "diff " + pat + " -r " + source + " " + target
print bashCommand
当我手动打印输出和 运行 命令时,它按预期工作。但是 Python 脚本没有产生预期的结果
$python BashCommand.py
diff -x ".*" -r /Users/abc/1/ /Users/abc/2/
Directories do not match: Only in /Users/abc/1/: .a
$diff -x ".*" -r /Users/abc/1/ /Users/abc/2/
$
在bash中,单引号和双引号的含义不同。来自 Difference between single and double quotes in Bash
Enclosing characters in single quotes (') preserves the literal value of each character within the quotes.
而对于双引号:
The special parameters * and @ have special meaning when in double quotes (see Shell Parameter Expansion).
因此您的 ".*"
在通过 diff
之前正在扩展。尝试切换引号
bashCommand = "diff -x '.*' -r " + source + ' ' + target
编辑
Popen 通常不使用 shell 来执行你的命令(除非你传递 shell=True
)所以你实际上根本不需要转义模式:
>>> subprocess.Popen(['diff', '-x', "'.*'", '-r', 'a', 'b'])
<subprocess.Popen object at 0x10c53cb50>
>>> Only in a: .dot
>>> subprocess.Popen(['diff', '-x', '.*', '-r', 'a', 'b'])
<subprocess.Popen object at 0x10c53cb10>