Process substitution not allowed by Python's subprocess with shell=True?

吃可爱长大的小学妹 提交于 2019-12-02 23:13:26

问题


Here is a toy example of process substitution that works fine in Bash:

$ wc -l <(pwd)
1 /proc/self/fd/11

So why does the same command give a syntax error when invoked from Python's subprocess with shell=True?

>>> subprocess.check_call('wc -l <(pwd)', shell=True)
/bin/sh: 1: Syntax error: "(" unexpected
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/path/to/my/python/lib/python3.5/subprocess.py", line 581, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command 'wc -l <(pwd)' returned non-zero exit status 2

回答1:


/bin/sh: 1: Syntax error: "(" unexpected

You have a bashism. It is not valid according to POSIX, which is what /bin/sh implements.




回答2:


An alternate solution is to shift more of the shell code to Python itself. For example:

from subprocess import Popen, PIPE, check_call

p1 = Popen(["pwd"], stdout=PIPE)
p2 = check_call(["wc", "-l"], stdin=p1.stdout)

This could often be the first step towards eliminating the need to use subprocess at all, as it decomposes the work into smaller chunks for which you may more readily see how to do in Python itself.



来源:https://stackoverflow.com/questions/39797234/process-substitution-not-allowed-by-pythons-subprocess-with-shell-true

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!