subprocess.Popen takes a list of arguments:
from subprocess import Popen, PIPE
process = Popen(['swfdump', '/tmp/filename.swf', '-d'], stdout=PIPE, stderr=PIPE)
stdout, stderr = process.communicate()
There's even a section of the documentation devoted to helping users migrate from os.popen to subprocess.
subprocess.Popen takes a list of arguments:
from subprocess import Popen, PIPE
process = Popen(['swfdump', '/tmp/filename.swf', '-d'], stdout=PIPE, stderr=PIPE)
stdout, stderr = process.communicate()
There's even a section of the documentation devoted to helping users migrate from os.popen to subprocess.
In the recent Python version, subprocess has a big change. It offers a brand-new class Popen to handle os.popen1|2|3|4.
The new subprocess.Popen()
import subprocess
subprocess.Popen('ls -la', shell=True)
Its arguments:
subprocess.Popen(args,
bufsize=0,
executable=None,
stdin=None, stdout=None, stderr=None,
preexec_fn=None, close_fds=False,
shell=False,
cwd=None, env=None,
universal_newlines=False,
startupinfo=None,
creationflags=0)
Simply put, the new Popen includes all the features which were split into 4 separate old popen.
The old popen:
Method Arguments
popen stdout
popen2 stdin, stdout
popen3 stdin, stdout, stderr
popen4 stdin, stdout and stderr
You could get more information in Stack Abuse - Robert Robinson. Thank him for his devotion.
Please help me understand how subprocess.popen works
How to get output from subprocess.Popen along with visible execution?
subprocess.Popen and output
Getting output from subprocess.Popen() while the command is being executed
Videos
I'm trying to grok subprocess.Popen to run and monitor a command line executable in the background. (Specifically, the HandbrakeCLI video converter.)
In this I have been partially successful, using the following command:
handbrake = subprocess.Popen( cmd )
Where cmd is a list of parameters. When I do this, I can do other things while it's running, poll() it, and terminate it if desired, and unless I kill it, it runs to completion exactly the way I want. The problem is that I also want to suppress the output. No problem, right?
handbrake = subprocess.Popen( cmd, stdout=subprocess.PIPE )
This works, but there's still SOME output. Now, my first thought was that the messages I was seeing were techincally ERROR messages. So I tried two different methods:
handbrake = subprocess.Popen( cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE )handbrake = subprocess.Popen( cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT )
In both cases, the subprocess launches, I can do other things, but... it doesn't do anything. I can see HandbrakeCLI in the task manager, but it's not using any resources (where it *should* be using nearly 100%), and no file has been created in the target directory.
This leaves me with two questions that may or may not be related:
-
Why is redirecting stdout causing the program to do nothing?
-
Where is that other output coming from, and how do I suppress it? (Helpfully, it doesn't contain any information I need to capture.)