我正在尝试使用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?
问候
答案 0 :(得分:2)
从"--exclude='{}'".format(...)
删除单引号,使用"--exclude={}".format(...)
如果不使用shell=True
,则不需要使用转义符。引号只对shell有意义,它们在执行命令之前被删除以创建参数列表。如果您在Popen
的参数列表中使用引号,则会将它们作为文字qoute字符传递,因此在您的情况下,您将排除包括引号的'Build.tar.gz'
。