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.
-
Do the examples in the docs help? golang.org/pkg/os/execMr_Pink– Mr_Pink2016-02-20 20:06:24 +00:00Commented Feb 20, 2016 at 20:06
-
nope, unfortunately I searched everywhere for some info but I can't run that Python script from GoZsolt Bokor– Zsolt Bokor2016-02-20 20:30:53 +00:00Commented Feb 20, 2016 at 20:30
-
If following the documentation doesn't help, you need to show what you've tried that isn't working, what the error is, and what you expect to happen. Otherwise we're just rewriting the same examples from the docs.Mr_Pink– Mr_Pink2016-02-20 20:33:33 +00:00Commented Feb 20, 2016 at 20:33
-
You seem to be using windows - are those paths correctly escaped?tumdum– tumdum2016-02-20 20:43:41 +00:00Commented Feb 20, 2016 at 20:43
Add a comment
|
1 Answer
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>