Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

getting a string as the return value from a system command

7 views
Skip to first unread message

Catherine Moroney

unread,
Apr 16, 2010, 3:06:13 PM4/16/10
to
Hello,

I want to call a system command (such as uname) that returns a string,
and then store that output in a string variable in my python program.

What is the recommended/most-concise way of doing this?

I could always create a temporary file, call the "subprocess.Popen"
module with the temporary file as the stdout argument, and then
re-open that temporary file and read in its contents. This seems
to be awfully long way of doing this, and I was wondering about
alternate ways of accomplishing this task.

In pseudocode, I would like to be able to do something like:
hostinfo = subprocess.Popen("uname -srvi") and have hostinfo
be a string containing the result of issuing the uname command.

Thanks for any tips,

Catherine

Robert Kern

unread,
Apr 16, 2010, 4:09:01 PM4/16/10
to pytho...@python.org
On 2010-04-16 14:06 PM, Catherine Moroney wrote:
> Hello,
>
> I want to call a system command (such as uname) that returns a string,
> and then store that output in a string variable in my python program.
>
> What is the recommended/most-concise way of doing this?
>
> I could always create a temporary file, call the "subprocess.Popen"
> module with the temporary file as the stdout argument, and then
> re-open that temporary file and read in its contents. This seems
> to be awfully long way of doing this, and I was wondering about
> alternate ways of accomplishing this task.

p = subprocess.Popen(['uname', '-srvi'], stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout, stderr = p.communicate()

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Catherine Moroney

unread,
Apr 16, 2010, 4:46:15 PM4/16/10
to
Robert Kern wrote:
> On 2010-04-16 14:06 PM, Catherine Moroney wrote:
>> Hello,
>>
>> I want to call a system command (such as uname) that returns a string,
>> and then store that output in a string variable in my python program.
>>
>> What is the recommended/most-concise way of doing this?
>>
>> I could always create a temporary file, call the "subprocess.Popen"
>> module with the temporary file as the stdout argument, and then
>> re-open that temporary file and read in its contents. This seems
>> to be awfully long way of doing this, and I was wondering about
>> alternate ways of accomplishing this task.
>
> p = subprocess.Popen(['uname', '-srvi'], stdout=subprocess.PIPE,
> stderr=subprocess.PIPE)
> stdout, stderr = p.communicate()
>

Thanks, I knew there had to be a more elegant way of doing that.

Catherine

Larry Hudson

unread,
Apr 16, 2010, 10:09:02 PM4/16/10
to

import os
txt = os.popen("uname -srvi")
hostinfo = txt.readline()

Or if the command outputs a number of lines (such as 'ls'),
use txt.readlines() to put the result into a list of strings.

-=- Larry -=-

Robert Kern

unread,
Apr 17, 2010, 12:24:08 PM4/17/10
to pytho...@python.org
On 2010-04-17 01:49 , CHEN Guang wrote:
> > Catherine Moroney wrote:
> >> Hello,
> >>
> >> I want to call a system command (such as uname) that returns a string,
> >> and then store that output in a string variable in my python program.
> >>
> >> What is the recommended/most-concise way of doing this?
> >>
> >> I could always create a temporary file, call the "subprocess.Popen"
> >> module with the temporary file as the stdout argument, and then
> >> re-open that temporary file and read in its contents. This seems
> >> to be awfully long way of doing this, and I was wondering about
> >> alternate ways of accomplishing this task.
> >>
> >> In pseudocode, I would like to be able to do something like:
> >> hosti nfo = subprocess.Popen("uname -srvi") and have hostinfo

> >> be a string containing the result of issuing the uname command.
> >>
> >> Thanks for any tips,
> >>
> >> Catherine
> >
> > import os
> > txt = os.popen("uname -srvi")
> > hostinfo = txt.readline()
> >
> > Or if the command outputs a number of lines (such as 'ls'),
> > use txt.readlines() to put the result into a list of strings.
> >
> > -=- Larry -=-
> >
> os.popen3() gives not only result but also error prompt (in case an
> error or warning happens)
> stdin,stdout,stderr = os.popen3('uname -srvi')
> resultText = stdout.read()
> errorText = stderr.read()
> For more examples of os.popen3() please look at source code of PythoidC
> (http://pythoidc.googlecode.com or http://pythoidc.sf.net )

The subprocess module is the preferred choice over either of those functions.

TomF

unread,
Apr 18, 2010, 4:13:41 AM4/18/10
to

Here is the way I do it:

import os
hostinfo = os.popen("uname -srvi").readline().strip()

(I add a strip() call to get rid of the trailing newline.)

os.popen has been replaced by the subprocess module, so I suppose the
new preferred method is:

from subprocess import Popen, PIPE
hostinfo = Popen(["uname", "-srvi"], stdout=PIPE).communicate()[0].strip()


Looks ugly to me, but there we are.

-Tom

Robert Kern

unread,
Apr 18, 2010, 7:10:05 PM4/18/10
to pytho...@python.org

The easy way to fix things that look ugly but are the right thing to do is to
wrap them up into a utility function and call the utility function everywhere.

0 new messages