Go and python call other programs and get program output

Source: Internet
Author: User

In C language, you can use the system function to call system commands and obtain the output. You can also save the output of program execution to a file for use through output redirection, but it is not very convenient to use. I will introduce how to use python and go to save the output of other programs as variables.

The following example uses the ls name. You need to install MinGW and add "C: \ MinGW \ msys \ 1.0 \ bin" to the environment variable.

1. Call other programs in python and obtain the output

Sample Code:

Copy codeThe Code is as follows:
Import OS
Var = OS. popen ('LS-l'). read ()
Print var

Running Effect (take my machine as an example ):

2. Use the go language to call other programs and obtain the output

Go code:

Copy codeThe Code is as follows:
Package main

Import (
"Exec" // "OS/exec" in go1
"Fmt"
)

Func main (){
Cmd: = exec. Command ("ls", "-l ")
Buf, err: = cmd. Output ()
Fmt. Printf ("% s \ n % s", buf, err)
}

The running effect is as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.