os.popen and the subprocess module
nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de
Thu Nov 29 10:09:44 CET 2012
Am 27.11.2012 19:00 schrieb Andrew:
> I'm looking into os.popen and the subprocess module, implementing
> os.popen is easy but i hear it is depreciating however I'm finding the
> implemantation of subprocess daunting can anyone help
This is only the first impression.
subprocess is much more powerful, but you don't need to use the full power.
For just executing and reading the data, you do not need much.
First step: create your object and performing the call:
sp = subprocess.Popen(['program', 'arg1', 'arg2'], stdout=subprocess.PIPE)
sp = subprocess.Popen('program arg1 arg2', shell=True,
The variant with shell=True is more os.popen()-like, but has security
flaws (e.g., what happens if there are spaces or, even worse, ";"s in
the command string?
Second step: Obtain output.
Here you either can do
stdout, stderr = sp.communicate()
can be used if the whole output fits into memory at once or you really
have to deal with stderr or stdin additionally.
In other, simpler cases, it is possible to read from sp.stdout like from
a file (with a for loop, with .read() or whatever you like).
Third step: Getting the status, terminating the process.
And if you have read the whole output, you do status = sp.wait() in
order not to have a zombie process in your process table and to obtain
the process status.
More information about the Python-list