Is there a substitution of Python's "os.system" in golang?

1,341 views
Skip to first unread message

Hailei Hu

unread,
Nov 8, 2014, 9:17:06 AM11/8/14
to golan...@googlegroups.com
I know there is a package named "os/exec" to run external command. But It is a little different from Python's "os.system".
for example, if I run "ping www.google.com" using Python's "os.system", the output will be continuously refreshed just like I am running it in a cmd.exe or shell.
If I use "os/exec" package, result will not be outputed unitl the command exits.

nvcnvn

unread,
Nov 8, 2014, 10:06:52 AM11/8/14
to golan...@googlegroups.com
I belive with StdoutPie you can "listen" the output like a stream.

Staven

unread,
Nov 8, 2014, 10:09:58 AM11/8/14
to golan...@googlegroups.com
On Sat, Nov 08, 2014 at 06:17:06AM -0800, Hailei Hu wrote:
> If I use "os/exec" package, result will not be outputed unitl the command
> exits.

Are you using Cmd.Output()? Take look at Cmd.Start() and Cmd.StdoutPipe() instead.

Robert Carlsen

unread,
Nov 8, 2014, 10:15:26 AM11/8/14
to golan...@googlegroups.com
Something like this is probably more what you are after: http://play.golang.org/p/rTlcyZ0UZo

Justin Israel

unread,
Nov 8, 2014, 1:55:04 PM11/8/14
to Robert Carlsen, golan...@googlegroups.com
And actually, this is really similar to pythons subprocess module, which is what you are supposed to be using in Python now, instead of all the other old options like os.system, os.Popen*, and the commands module.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Hailei Hu

unread,
Nov 9, 2014, 2:50:36 AM11/9/14
to golan...@googlegroups.com
Thanks for all the replies.
I tried the example in "http://play.golang.org/p/rTlcyZ0UZo", and this is really what I want.
Thanks again

ns...@google.com

unread,
Nov 9, 2014, 5:02:49 AM11/9/14
to golan...@googlegroups.com
The exec package is way more powerful than it first looks. Try this:


If you want to interactively process the output, you *still* don't need StdoutPipe. Do this:

Reply all
Reply to author
Forward
0 new messages