To get ls output, use stdout=subprocess.PIPE .
>>> proc = subprocess.Popen('ls', stdout=subprocess.PIPE) >>> output = proc.stdout.read() >>> print output bar baz foo
The cdrecord --help command prints to stderr, so you need to connect this node. You should also split the command into a list of tokens, as I did below, or an alternative is to pass the shell=True argument, but it launches a completely bloated shell, which can be dangerous if you do not control the contents of the command line.
>>> proc = subprocess.Popen(['cdrecord', '--help'], stderr=subprocess.PIPE) >>> output = proc.stderr.read() >>> print output Usage: wodim [options] track1...trackn Options: -version print version information and exit dev=target SCSI target to use as CD/DVD-Recorder gracetime=
If you have a command that outputs to stdout and stderr, and you want to combine them, you can do this by going stderr to stdout and then catching stdout.
subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
As mentioned by Chris Morgan , you should use proc.communicate() instead of proc.read() .
>>> proc = subprocess.Popen(['cdrecord', '--help'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) >>> out, err = proc.communicate() >>> print 'stdout:', out stdout: >>> print 'stderr:', err stderr:Usage: wodim [options] track1...trackn Options: -version print version information and exit dev=target SCSI target to use as CD/DVD-Recorder gracetime=
marcog Dec 22 '10 at 23:49 2010-12-22 23:49
source share