Python 子进程、子 shell 和重定向

标签 python bash subprocess subshell

我想使用 subshel​​l 的魔力和 python 子进程模块的重定向,但它似乎不起作用,提示意外的标记是括号。例如命令

cat <(head tmp)

当传递给子进程时给出这个

>>> subprocess.Popen("cat <(head tmp)", shell=True)
<subprocess.Popen object at 0x2b9bfef30350>
>>> /bin/sh: -c: line 0: syntax error near unexpected token `('
/bin/sh: -c: line 0: `cat <(head tmp)'

最佳答案

<(head tmp)语法是 bash称为“进程替换”的功能。基本/便携/bin/sh不支持它。 (即使在 /bin/sh/bin/bash 是同一个程序的系统上也是如此;当以普通 /bin/sh 调用时它不允许此功能,因此您不会无意中依赖非可移植功能。)

>>> subprocess.Popen(["/bin/bash", "-c", "cat <(head tmp)"])
<subprocess.Popen object at 0x1004cca50>

关于Python 子进程、子 shell 和重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7407667/

相关文章:

php - 如何使用 Git 在开发和生产环境中分发 Thrift 生成的代码?

python - 瓶静态文件

python - 在开发 Spark 应用程序时使用多种语言

bash - 虽然真正的 Breaking Bash

python - Pyinstaller 在 Windows 下的 onefile 可执行文件上出现 subprocess.check_output 问题

Python - 执行进程 ->阻塞直到它退出并抑制输出

python - 如何将 python "login shell"与 "call"一起使用?

python - 在 python 中获取空 CSV

linux - ubuntu 服务器上的参数太多

bash - $GEM_PATH 通常设置在哪里?