execute commands and return output

B

billiejoex

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
 
T

tiissa

billiejoex said:
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
 
L

Leif K-Brooks

billiejoex said:
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())
 
F

Fredrik Lundh

billiejoex said:
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>
 
B

billiejoex

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
 
D

Do Re Mi chel La Si Do

Hi !

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

@-salutations

Michel Claveau
 
F

Fredrik Lundh

billiejoex said:
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>
 
L

Leif K-Brooks

billiejoex said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top