Tar --exclude 在 popen(shell=False) 时不起作用

Tar --exclude not working when popen(shell=False)

我正在尝试使用 Popen 执行以下命令。

args = ['/bin/tar', "--exclude='{}'".format('Build.tar.gz'), '-capvf', targetFile, '.' ]
popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)

然而,排除部分只有在我设置 shell=True IE 运行 时才有效,上面的命令不会排除文件 'Build.tar.gz'。 这是为什么? 有没有办法在不设置 shell=True 的情况下完成这项工作?

问候

去掉"--exclude='{}'".format(...)中的单引号,使用"--exclude={}".format(...)

当您不使用 shell=True 时,您不需要使用转义符。引号仅对 shell 有意义,它们在执行命令以创建参数列表之前被删除。如果您在 Popen 的参数列表中使用引号,它们将作为文字引号字符传递,因此在您的情况下,您将排除 'Build.tar.gz' 包括引号。