我正在尝试使用Go获取我的Python版本:
import (
"log"
"os/exec"
"strings"
)
func verifyPythonVersion() {
_, err := exec.LookPath("python")
if err != nil {
log.Fatalf("No python version located")
}
out, err := exec.Command("python", "--version").Output()
log.Print(out)
if err != nil {
log.Fatalf("Error checking Python version with the 'python' command: %v", err)
}
fields := strings.Fields(string(out))
log.Print(fields)
}
func main() {
verifyPythonVersion()
}
Run Code Online (Sandbox Code Playgroud)
这将返回空切片:
2014/01/03 20:39:53 []
2014/01/03 20:39:53 []
Run Code Online (Sandbox Code Playgroud)
知道我做错了什么吗?
$ python --version
Python 2.7.2
$ python --version 1>/dev/null # hide stdout
Python 2.7.2
$ python --version 2>/dev/null # hide stderr
Run Code Online (Sandbox Code Playgroud)
我们可以得出结论,输出转到stderr.现在我看看Go的文档,并猜测是什么,cmd.Output只捕获stdout(docs).你应该使用cmd.CombinedOutput(docs):
CombinedOutput运行命令并返回其组合标准输出和标准错误.
| 归档时间: |
|
| 查看次数: |
127 次 |
| 最近记录: |