A shell-like DSL front for subprocess.Popen
The popen package provides a simple shell-like syntax inside python for running external commands.
pip install popen
from popen import Sh for line in Sh('du', '~/') | 'head -n 10' | 'sort -n': print('GOT', line)
Simples usage, run a command and iterate the output lines:
from popen import Sh for line in Sh('ls', '-la', '~'): print line
Piping that output to a file:
Sh('ls', '-la', '~') > '~/listing'
Note that special characters like ~ are expanded, as are environment variables (e.g. $HOME).
Chaining commands is trivial, here with append instead of write to file:
Sh('ls') | Sh('sort') >> '~/listing'
But the right hand side of | can be very flexible, as it employs lexical splitter on string input or takes an iterable:
Sh('ls', '-la', '~') | 'sort -c' | ['uniq', '-c'] | 'tail' | Sh('wc') > '~/listing'
To run a command and let output go to stdout, ask for the return code:
cmd = Sh('ls') | 'grep polka' print cmd.returncode
To pipe a string into a command, wrap in an iterable:
print Sh.pipe(['this is a \nmulti line\nstring.']) | 'wc'