如何使用exec命令从golang向python发送字节数据?
梅因,加油 我想发送字节数组作为python脚本的输入 abc.py如何使用exec命令从golang向python发送字节数据?,python,go,io,stdin,Python,Go,Io,Stdin,梅因,加油 我想发送字节数组作为python脚本的输入 abc.py func main() { bytearray=getbytearray()//getting an array of bytes cmd := exec.Command("python3", "abc.py") in:=cmd.Stdin cmd.Run() } 如何从golang向python发送字节并将其保存到文件中?您可以通过调用exec.Command访问进程的stdin。这将为您提
func main() {
bytearray=getbytearray()//getting an array of bytes
cmd := exec.Command("python3", "abc.py")
in:=cmd.Stdin
cmd.Run()
}
如何从golang向python发送字节并将其保存到文件中?您可以通过调用
exec.Command
访问进程的stdin。这将为您提供一个在进程终止时自动关闭的窗口
对stdin的写入必须在cmd.Run
调用的另一个goroutine中完成
下面是一个将“Hi There!”(作为字节数组)写入stdin的简单示例
import sys
newFile.write(sys.stdin) //write the byte array got as input to the newfile
您还需要实际读取python中的stdin:
package main
import (
"fmt"
"os/exec"
)
func main() {
byteArray := []byte("hi there!")
cmd := exec.Command("python3", "abc.py")
stdin, err := cmd.StdinPipe()
if err != nil {
panic(err)
}
go func() {
defer stdin.Close()
if _, err := stdin.Write(byteArray); err != nil {
panic(err)
}
}()
fmt.Println("Exec status: ", cmd.Run())
}
好的,谢谢。我正在从.wav文件读取字节数组并发送字节。我似乎无法将数据保存在python中的新.wav文件中。您可能需要以二进制模式
wb
()打开该文件。另外,不要忘记关闭文件。如果这是python的全部功能,为什么不使用Go保存文件呢?
import sys
f = open('output', 'w')
f.write(sys.stdin.read())