Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python subprocess, subshells, and redirection

I want to use the magic of subshells and redirection with the python subprocess module, but it doesn't seem to work, complaining about unexpected tokens are the parenthesis. For example, the command

cat <(head tmp)

when passed to subprocess gives this

>>> 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)'
like image 827
pythonic metaphor Avatar asked Sep 13 '11 19:09

pythonic metaphor


1 Answers

The <(head tmp) syntax is a bash feature called "process substitution". The basic/portable /bin/sh doesn't support it. (This is true even on systems where /bin/sh and /bin/bash are the same program; it doesn't allow this feature when invoked as plain /bin/sh so you won't inadvertently depend on a non-portable feature.)

>>> subprocess.Popen(["/bin/bash", "-c", "cat <(head tmp)"])
<subprocess.Popen object at 0x1004cca50>
like image 143
Scott Lamb Avatar answered Sep 28 '22 15:09

Scott Lamb