Execute python file with GoLang?

I’m trying to execute a python file using GoLang however I’m running windows and it doesn’t like the backslashes. I’ve searched the web but there are no clear and general answers. I get the error %1 is not a valid win32 application.

I would appreciate any help. Thanks.

  package main

    import (
        "os/exec"
        "fmt"
    )

    func main() {
        cmd := exec.Command("C:\\Users\\me\\go\\test.py")
        output, err := cmd.Output()

        if (err != nil) {
            fmt.Println(err)
        } 
        fmt.Println(string(output))
    }

My guess is that you need to pass test.py as an argument to python.exe:

cmd := exec.Command("C:\\path\\to\\python.exe", "C:\\Users\\me\\go\\test.py")
3 Likes

Awesome! That was just the solution, thank you very much!!!

1 Like

Also, note that you can use backticks for a raw string:

"C:\\path\\to" == `C:\path\to`
2 Likes

One can even use forward slashes nowadays even on Windows

3 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.