1.go调用python脚本
#go调用python有编码问题,GbkToUtf8是自己封装的转换编码
command := "build.py"
cmd := exec.Command("python", command, name)
output, err := cmd.Output()
if err != nil {
fmt.Printf("Execute Shell:%s failed with error:%s", command, err.Error())
return
}
txt, _ := utils.GbkToUtf8(output)
fmt.Printf("Execute Shell:%s finished with output:\n%s", command, string(txt))
#go调用python直接报错status=-1,CombinedOutput 可以看到实际输出
cmd := exec.Command("python", command)
output, err := cmd.CombinedOutput()
if err != nil {
fmt.Println(fmt.Sprint(err) + ": " + string(output))
return
}
fmt.Println(string(output))
#GbkToUtf8 方法
import (
"bytes"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
"io/ioutil"
)
func GbkToUtf8(str []byte) (b []byte, err error) {
r := transform.NewReader(bytes.NewReader(str), simplifiedchinese.GBK.NewDecoder())
b, err = ioutil.ReadAll(r)
if err != nil {
return
}
return
}
python内部读取文件目录报错
from pathlib import Path
import os
path1 = os.path.abspath(os.path.dirname(__file__))
with open(Path(path1) / settings.DATASET_PATH, 'r', encoding='utf-8') as f: