Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #34055

Re: os.popen and the subprocess module

From Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de>
Newsgroups comp.lang.python
Subject Re: os.popen and the subprocess module
Date 2012-11-29 10:09 +0100
Organization A newly installed InterNetNews server
Message-ID <k978ot$b04$1@r03.glglgl.gl> (permalink)
References <mailman.316.1354039468.29569.python-list@python.org>

Show all headers | View raw


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)

or

sp = subprocess.Popen('program arg1 arg2', shell=True, 
stdout=subprocess.PIPE)


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.


Thomas

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

os.popen and the subprocess module Andrew <drew00andy@yahoo.co.uk> - 2012-11-27 20:00 +0200
  Re: os.popen and the subprocess module Tim Roberts <timr@probo.com> - 2012-11-28 20:14 -0800
  Re: os.popen and the subprocess module Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2012-11-29 10:09 +0100
    Re: os.popen and the subprocess module Nobody <nobody@nowhere.com> - 2012-11-29 18:39 +0000
      Re: os.popen and the subprocess module emile <emile@fenx.com> - 2012-11-30 10:01 -0800

csiph-web