我尝试了一些替代方案,但都没有令人满意。我发现最好的是切换到Popen
。
# this should have the a similar signature to check_call
def run_in_shell(*args):
# unfortunately, `args` won't be escaped as it is actually a string argument to bash.
proc = subprocess.Popen(['/bin/bash', '-c', ' '.join(args)])
# This will also work, though I have found users who had problems with it.
# proc = subprocess.Popen(' '.join(args), shell=True, executable='/bin/bash')
stat = proc.wait()
if stat != 0:
subprocess.CalledProcessError(returncode=stat, cmd=command)
return stat
run_in_shell("cat parsing.tgz <(echo -n ''| gzip)> new-file.tgz")
作为说明:/bin/sh
与未转义的括号存在问题。如果你不希望上述指定'/bin/bash'
,则需要逃跑的括号:
args = 'cat parsing.tgz <\\(echo -n ''| gzip\\)> new-file.tgz'
“不工作”是什么意思? – Chris
@Chris;我更新了我的问题,具体到您的意见。 – Viswesn
关闭主题,但不是使用'split()',而应该使用['shlex.split()'](https://docs.python.org/3.5/library/shlex.html#shlex.split)。 – squiguy