使用Go获取Python版本
发布时间:2020-12-16 23:00:33 所属栏目:Python 来源:网络整理
导读:我正在尝试使用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").
我正在尝试使用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() } 这将返回空切片: 2014/01/03 20:39:53 [] 2014/01/03 20:39:53 [] 知道我做错了什么吗? 解决方法$python --version Python 2.7.2 $python --version 1>/dev/null # hide stdout Python 2.7.2 $python --version 2>/dev/null # hide stderr 我们可以得出结论,输出转到stderr.现在我看看Go的文档,并猜测是什么,cmd.Output只捕获stdout(docs).你应该使用cmd.CombinedOutput(docs):
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |