Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

go - Passing parameters to cmd.exec function

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Thanks for the comments provided, the issue is because both output folder having the ext .pdf so the pdf2txt understand that I'm converting 2 pdf with the same name.

To fix it, I removed the ext .pdf from the first string which is to be used for the output directory name using strings.Split so my code became:

    for _, file := range files {
        fmt.Println(file)
        s := strings.Split(file, ".")
        cmd := exec.Command("pdf2text", "-o", s[0], file)
        err := cmd.Run()
        if err != nil {
            log.Fatalf("cmd.Run() failed with %s
", err)
        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...