You are not logged in.

#1 2007-03-23 23:26:40

kamagurka
Member
From: Munich, Germany
Registered: 2006-02-20
Posts: 150
Website

Python: taking the output from an external command [SOLVED]

I want to get the output from an external command, put it in a variable and use it later in an if statement like so:

variable = os.system("dcop amarok player osdEnabled")
#that dcop command returns either 'true' or 'false'
if variable == "true":
   do something
else:
   do something else

It malfunctions in weird and interesting ways, and I think I'm going about this all wrong. I'm suspecting os.system is maybe the wrong way of getting the output of a command and stuffing it into a variable, since when I try printing variable, I get nothing, but I couldn't find the right way in the documentation. I also read that maybe the new subprocesses module might be the way to go, but the documentation for that made my eyes water.

I sat down because I thought I might dust off my old python "skill" and knock out a quick ten-liner, but now I've been at it for two hours and nothing works. I'm just glad I don't code for a living, or I'd be living out of a cardboard box...

Last edited by kamagurka (2007-03-24 14:47:42)


I always roll 20s on my disbelieve checks.
You better believe it.

Offline

#2 2007-03-24 01:00:07

iphitus
Forum Fellow
From: Melbourne, Australia
Registered: 2004-10-09
Posts: 4,927

Re: Python: taking the output from an external command [SOLVED]

os.system returns not the output, but the return code of the external program run.

Instead you ought to use either commands (http://docs.python.org/lib/module-commands.html) or subprocess (http://docs.python.org/lib/module-subprocess.html)

Subprocess is intended to replace commands, so it's suggested you use it instead. An example which does what you would like is here: http://docs.python.org/lib/node535.html

Though, if you're dealing with the output of scripts, it may be easier to use bash.

James

Offline

#3 2007-03-24 14:27:48

kamagurka
Member
From: Munich, Germany
Registered: 2006-02-20
Posts: 150
Website

Re: Python: taking the output from an external command [SOLVED]

Maybe I'm being dense, but python doesn't seem to understand that example you gave me too well.
If I run this

variable = subprocess.Popen(["dcop amarok player osdEnabled"], stdout=PIPE)

(which by the way I don't understand in the least), I get that the name PIPE is not defined. If I leave all the stuff that I don't understand out (namely, whatever comes after the bracket closes), I get this darling of an error:

Traceback (most recent call last):
  File "/home/kamagurka/hda/eigene_dateien/python/amarok_toggle_osd.py", line 15, in <module>
    osd_status = subprocess.Popen("dcop amarok player osdEnabled")
  File "/usr/lib/python2.5/subprocess.py", line 593, in __init__
    errread, errwrite)
  File "/usr/lib/python2.5/subprocess.py", line 1051, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

Do I need to do some additional voodoo beyond just "import subprocess"? Or what?


EDIT:

Though, if you're dealing with the output of scripts, it may be easier to use bash.

Well, I'm not dealing with the output of a script. I'm dealing with a command that can only return one of two words. I really thought that was going to be the simplest thing about this.

EDIT: I thought I'd maybe try the old way, and lo and behold, the "old" commands module gives me exactly the tools to do what I need to do in a simple, clean way. Does anyone have any idea why it's supposed to be replaced by subprocesses? Because, as far as I can tell, that thing is a fucking mess. I mean, did you look at the "replacing" section? The "new" way to do stuff is always twice as long and looks like perl, for pete's sake.

Last edited by kamagurka (2007-03-24 14:40:31)


I always roll 20s on my disbelieve checks.
You better believe it.

Offline

#4 2007-03-24 14:52:49

bboozzoo
Member
From: Poland
Registered: 2006-08-01
Posts: 125

Re: Python: taking the output from an external command [SOLVED]

seems to work just fine:

>>> import subprocess
>>> c = subprocess.Popen(["ls"], stdout = subprocess.PIPE)
>>> c
<subprocess.Popen object at 0xb7be8d6c>
>>> c.communicate()
('CProgramming.tar.gz\nDesktop\nDoom\nF-6.91-i386-DVD.iso\nOSX3.3.tar.gz\nPKGBUILD~\nbackdrops\nbboozzoo\nbin\nbuilds\ndebian-testing-i386-netinst.iso\nemu\nfed.url\nftp-0.8-beta2-20070222-i686.iso\ngimp\nimg_jabber.jpg\nubuntu-6.10-desktop-i386.iso\nvim.tar.gz\nworkspace\nzdjecia\n', None)

Last edited by bboozzoo (2007-03-24 15:01:37)

Offline

#5 2007-03-24 15:19:17

T-Dawg
Forum Fellow
From: Charlotte, NC
Registered: 2005-01-29
Posts: 2,736

Re: Python: taking the output from an external command [SOLVED]

import subprocess
c = subprocess.Popen(["ls"], stdout = subprocess.PIPE).wait()
output = c.stdout.read()
c.stdout.close()
print c

Offline

#6 2007-03-24 15:32:15

kamagurka
Member
From: Munich, Germany
Registered: 2006-02-20
Posts: 150
Website

Re: Python: taking the output from an external command [SOLVED]

Ah, I see where I went wrong. PIPE needs a subprocess in front, too.

import commands
foo = commands.getoutput("ls")
print foo

is a lot simpler, and overall more beautiful. Or does T-Dawg's way have some advantage that I missed?


I always roll 20s on my disbelieve checks.
You better believe it.

Offline

#7 2007-03-24 15:43:22

T-Dawg
Forum Fellow
From: Charlotte, NC
Registered: 2005-01-29
Posts: 2,736

Re: Python: taking the output from an external command [SOLVED]

For one they're pushing subproccess as the new default. Have a look at pydoc subprocess and you'll seen numerous conversion examples. Secondly subproccess is more rubust. It combines all the features of os.spawn, os.system and commands into one modules. Things like poll(), communicate(), wait(), stdin, stdout, stderr, pid, etc, are all available with subprocess. Again, have a look at the documentation.

Offline

#8 2007-03-24 17:14:30

kamagurka
Member
From: Munich, Germany
Registered: 2006-02-20
Posts: 150
Website

Re: Python: taking the output from an external command [SOLVED]

I know they're pushing it, and I know how many things it can do. Looking at the documentation, however, I see that the price for that is that nearly everything became more complicated. I prefer the unix way: something small and simple that does one thing well is better than a monolith.
Can you deny that commands is better for this specific task than subprocesses?


I always roll 20s on my disbelieve checks.
You better believe it.

Offline

Board footer

Powered by FluxBB