简体   繁体   English

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

[英]How to run external Python script in Golang?

I would like to run an external Python script that gets 4 arguments. 我想运行一个获取4个参数的外部Python脚本。 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. 如果我想在cmd中运行Python脚本,则应如下所示:python Required \\ Python \\ screenshot.py-master \\ screenshot.py --nojs -thumb http://google.com/ Required \\ Images \\ Screenshots \\ google.jpg因此,我想从Go运行此命令。 How could I implement this? 我该如何实施? Thanks. 谢谢。

If examples from doc are not helpful, maybe this will make it easier for you. 如果doc中的示例无济于事,也许这会让您更轻松。

test.go: 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: 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM