Monday 29 April 2013

executing shell commands from python

p = subprocess.call(['/bin/bash', '-c', 'ls *.pdf'], stdout=sub.PIPE, stderr=sub.STDOUT))

This is blocking.  For non-blocking use,

p = subprocess.Popen(['/bin/bash', '-c', 'ls *.pdf'], stdout=sub.PIPE, stderr=sub.STDOUT))


The p object has methods for interrogating the output from the command.

Python 2.7 has more options.  For < 2.7 use,


p = subprocess.Popen(['ls', '-l'], stdout=subprocess.PIPE).communicate()[0]
to get the output from the command.

No comments:

Post a Comment