By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,713 Members | 764 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,713 IT Pros & Developers. It's quick & easy.

execute commands and return output

P: n/a
Hi all. I'm searching for a portable (working on *nix and win32) function
that executes a system command and encapsulate its output into a string.
Searching for the web I found this:

os.popen('command').read()

It is perfect but when che command return an error the funciotn returns an
empy string.
Does it is possible to return stdout and stderr too?

Best regards

Sep 10 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
billiejoex wrote:
Hi all. I'm searching for a portable (working on *nix and win32) function
that executes a system command and encapsulate its output into a string.
Searching for the web I found this:

os.popen('command').read()

It is perfect but when che command return an error the funciotn returns an
empy string.
Does it is possible to return stdout and stderr too?


Use subprocess:

from subprocess import Popen, PIPE
proc = Popen(['command', 'arg', 'arg'], stdout=PIPE, stderr=PIPE)
return_code = proc.wait()
if return_code == 0:
print "Success:\n%s" % proc.stdout.read()
else:
print "Failure %s:\n%s" % (return_code, proc.stderr.read())
Sep 10 '05 #2

P: n/a
"billiejoex" wrote:
Hi all. I'm searching for a portable (working on *nix and win32) function that executes a system command and encapsulate its
output into a string.
Searching for the web I found this:

os.popen('command').read()

It is perfect but when che command return an error the funciotn returns an empy string.
Does it is possible to return stdout and stderr too?


see the variations popen2, popen3 and popen4:

http://docs.python.org/lib/os-newstreams.html

or use the subprocess module:

http://docs.python.org/lib/module-subprocess.html

</F>

Sep 10 '05 #3

P: n/a
Thank you for your help but I'm searching a different way.
Moreover it doesn't work always (for exaple: try a 'dir' command).
Because of I'm implementing a remote shell the
[[os.popen('command').read()]] rapresents the best for me because it can
also accepts arguments direclty (for example:
os.popen('netstat -a -n -o').read() and this is a great advantage.
I was looking at sys.stdout and sys.stderr. Can they be helpful?

Cheers
Use subprocess:

from subprocess import Popen, PIPE
proc = Popen(['command', 'arg', 'arg'], stdout=PIPE, stderr=PIPE)
return_code = proc.wait()
if return_code == 0:
print "Success:\n%s" % proc.stdout.read()
else:
print "Failure %s:\n%s" % (return_code, proc.stderr.read())

Sep 10 '05 #4

P: n/a
Hi !

Look (the doc for) Popen2, Popen3, Popen4 & Subprocess

@-salutations

Michel Claveau
Sep 10 '05 #5

P: n/a
"billiejoex" wrote:
Moreover it doesn't work always (for exaple: try a 'dir' command).


why use os.system("dir") when Python already offers things
like os.listdir, os.walk, and glob.glob ?

</F>

Sep 10 '05 #6

P: n/a
billiejoex wrote:
Thank you for your help but I'm searching a different way.
Moreover it doesn't work always (for exaple: try a 'dir' command).
Because of I'm implementing a remote shell the
[[os.popen('command').read()]] rapresents the best for me because it can
also accepts arguments direclty (for example:
os.popen('netstat -a -n -o').read() and this is a great advantage.


If you really need shell evaluation, try subprocess.Popen('foo',
shell=True) instead.
Sep 10 '05 #7

P: n/a
billiejoex wrote:
Hi all. I'm searching for a portable (working on *nix and win32) function
that executes a system command and encapsulate its output into a string.
Searching for the web I found this:

os.popen('command').read()

It is perfect but when che command return an error the funciotn returns an
empy string.
Does it is possible to return stdout and stderr too?


You may want to look at the subprocess [1] module and its Popen class [2].

[1] http://python.org/doc/2.4.1/lib/module-subprocess.html
[2] http://python.org/doc/2.4.1/lib/node234.html

Sep 10 '05 #8

P: n/a
Thanks for suggestions.
I'll try one of these solutions soon.
Sep 10 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.