dongmo3413 2016-02-20 19:46
浏览 535

如何在Golang中运行外部Python脚本?

I would like to run an external Python script that gets 4 arguments. If I would like to run the Python script in cmd it would look like this: python Required\Python\screenshot.py-master\screenshot.py --nojs -thumb http://google.com/ Required\Images\Screenshots\google.jpg So, I would like to run this command from Go. How could I implement this? Thanks.

  • 写回答

1条回答 默认 最新

  • dongtao9158 2016-02-20 20:40
    关注

    If examples from doc are not helpful, maybe this will make it easier for you.

    test.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())
    }
    

    test.py:

    import sys
    print sys.argv
    

    usage:

    $ 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>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 slam rangenet++配置
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊