python subprocess.call bash命令,带有进程替换和文件重定向

时间:2011-05-20 03:32:57

标签: python bash

在Python中,我想调用一个脚本,该脚本在执行时打印到屏幕,同时还记录stdout和stderr流。我有以下一行我要执行的bash代码:

script.sh > >(tee /tmp/success.log) 2> >(tee /tmp/errors.log >&2)

其中script.sh是输出到stdout和stderr的任何脚本。此行将stdout和stderr流捕获到日志文件success.log和errors.log中,并将流输出到stdout,如下所述:How do I write stderr to a file while using "tee" with a pipe?

现在我想在python中使用subprocess.call(that_command_as_str,shell = True)执行此操作,但是python使用/ bin / sh来执行命令而不是/ bin / bash,而vanilla sh不支持重定向的类型。似乎/ bin / sh的使用被硬编码到子进程模块中。任何想法?

1 个答案:

答案 0 :(得分:3)

将您的命令包裹在/bin/bash -c "your-command-stuff"

然后/ bin / sh将运行bash,它将为您解释其余部分。您还可以考虑使用Python Subprocess中的其他函数来进行重定向,但上述内容应该快速有效。

相关问题