我嘗試了一些替代方案,但都沒有令人滿意。我發現最好的是切換到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