473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

execute commands and return output

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
8 10076
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
"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
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
Hi !

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

@-salutations

Michel Claveau
Sep 10 '05 #5
"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
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
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
Thanks for suggestions.
I'll try one of these solutions soon.
Sep 10 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Christopher-Robin | last post by:
Hi I've got a problem and I hope someone can help me: I want a PHP script to call another PHP script, execute it and return its OUTPUT (not its content) into a STRING in the first script. ...
5
by: Mike Tammerman | last post by:
Hi, I am trying to execute an executable or a pyton script inside my program. I looked at the subprocess and os module. But all the functions in these modules blocks my application. What I want...
15
by: Madhanmohan S | last post by:
Hi All, I want to run a command line appplication from C#. When i start the application, it will go into specific mode. After that, i have to give commands to use the application. How Can This Be...
1
by: Faraz | last post by:
Hey guys. How cam I interface with the environment in C#. I want to execute commands in a (separate) process, just like java Runtime.exec(string command) does. Thanks in advance,
15
by: dylpkls91 | last post by:
I have been researching this topic and come up with some code to make it work. It uses SSL and requires the 3rd party package Paramiko (which requires PyCrypto). However, at this moment I have no...
1
by: jonathan184 | last post by:
Hi I am trying to login a JMS admin tool and execute JMS commands and using perl windows to do this. This is the e.g of connection string they said to use when trying to connec to jms ****connect ...
15
by: tmp123 | last post by:
Hello, Thanks for your time. We have very big files with python commands (more or less, 500000 commands each file). It is possible to execute them command by command, like if the commands...
1
by: Hishaam | last post by:
How to execute commands in internal zones of solaris using python running from the global zone ? i tried -- 1os.popen("zlogin <zone1>") 2os.popen("zonename") the 2nd command executes back...
1
by: nthato | last post by:
Hi I am trying to create a script that will execute commands on several remote servers and I am having problems to increment my positional variables. I the a simple way of doing this???
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.