在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?
现在我想使用subprocess.call(that_command_as_str,shell = True)从python内部执行此操作,但是python使用/ bin / sh而不是/ bin / bash来执行命令,而vanilla sh不支持这种重定向类型。似乎/ bin / sh的用法已硬编码到子进程模块中。有任何想法吗?
最佳答案
将命令包装在/bin/bash -c "your-command-stuff"
中。
然后/ bin / sh将运行bash,它将为您解释其余的内容。您也可以考虑在Python Subprocess中使用其他函数进行重定向,但是上述操作应该快速有效。
关于python - 带有进程替换和文件重定向的python subprocess.call bash命令,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6067173/