I want to read the text of multiple PDF files. I could not find proper Go lib, so I'm using PDF2Text tool, and wrote the below code:
package main
import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
)
func main() {
var files []string
root := "."
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
if filepath.Ext(path) != ".pdf" {
return nil
}
files = append(files, info.Name())
return nil
})
if err != nil {
panic(err)
}
for _, file := range files {
fmt.Println(file)
cmd := exec.Command("pdf2text", "-o", "files", file)
err := cmd.Run()
if err != nil {
log.Fatalf("cmd.Run() failed with %s
", err)
}
}
}
This is working file, and extracting all pdf files into folder "files", but as demo version of this tool is extraction the pdf file into multiple text file (file per page), I want to have the folder to which the PDF file is extracted to be same as the file name itself, so I tried replacing the:
cmd := exec.Command("pdf2text", "-o", "files", file)
By
cmd := exec.Command("pdf2text", "-o", file, file)
But it did not work, nothing had been executed, no error had been thrown.
question from:
https://stackoverflow.com/questions/65829916/passing-parameters-to-cmd-exec-function 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…