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 -- http://mail.python.org/mailman/listinfo/python-list
Re: execute commands and return output
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()) -- http://mail.python.org/mailman/listinfo/python-list
Re: execute commands and return output
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: execute commands and return output
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()) -- http://mail.python.org/mailman/listinfo/python-list
Re: execute commands and return output
Hi ! Look (the doc for) Popen2, Popen3, Popen4 Subprocess @-salutations Michel Claveau -- http://mail.python.org/mailman/listinfo/python-list
Re: execute commands and return output
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: execute commands and return output
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. -- http://mail.python.org/mailman/listinfo/python-list
Re: execute commands and return output
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: execute commands and return output
Thanks for suggestions. I'll try one of these solutions soon. -- http://mail.python.org/mailman/listinfo/python-list