我想运行一个获取 4 个参数的外部 Python 脚本。如果我想在 cmd 中运行 Python 脚本,它将如下所示:python Required\Python\screenshot.py-master\screenshot.py --nojs -thumb http://google.com/必需\图片\屏幕截图\google.jpg 所以,我想从 Go 运行这个命令。 我怎么能实现这个? 谢谢。
最佳答案
如果文档中的示例没有帮助,也许这会让您更轻松。
测试.go:
package main
import (
"log"
"os"
"os/exec"
)
func main() {
log.Println(os.Args)
if len(os.Args) == 1 {
return
}
cmd := exec.Command(os.Args[1], os.Args[2:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
log.Println(cmd.Run())
}
测试.py:
import sys
print sys.argv
用法:
$ go run test.go python test.py 1 two 3 four
2016/02/20 21:45:42 [/tmp/go-build772613382/command-line-arguments/_obj/exe/test python test.py 1 two 3 four]
['test.py', '1', 'two', '3', 'four']
2016/02/20 21:45:42 <nil>
关于python - 如何在 Golang 中运行外部 Python 脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35528180/