我无法像我想要的那样返回字符串切片。 只传递最后一个

I wrote this code to get the list of the file in directory, appending the names in a slice and one by one open them, after I open a file I search for some words in the file and if found write them in a new file. But I always get the same words in the new files and I can't figure out why

package main

import (
    "bufio"
    "fmt"
    "io/ioutil"
    "log"
    "os"
    "strings"
    "time"
)

const dir_to_read_path string = "path"

func main() {

    start := time.Now()
    temp_string_filename := ""
    temp_string_filename_counter := 0

    //defer list_file()

    // just pass the file name
    for k := range list_file() {

        temp_string_filename = list_file()[temp_string_filename_counter]
        if true {
            k = k
        }
        temp_string_filename_counter++

        b, err := ioutil.ReadFile(temp_string_filename)
        if err != nil {
            fmt.Print(err)
        }

        // convert content to a 'string'
        str := string(b)
        control_params := []string{"numpy", "grabscreen", "cv2", "time", "os", "pandas", "tqdm", "collections", "models", "random", "inception_v3", "googlenet", "shuffle", "getkeys", "tflearn", "directkeys", "statistics", "motion", "tflearn.layers.conv", "conv_2d", "max_pool_2d", "avg_pool_2d", "conv_3d", "max_pool_3d", "avg_pool_3d"}

        temp_string_filename = dir_to_read_path + "output_" + temp_string_filename

        fmt.Println("Writing file n. ", k)

        file, err := os.Create(temp_string_filename)
        if err != nil {
            log.Fatal("Cannot create file", err)
        }

        for _, z := range isValueInList(control_params, str, list_file()) {

            fmt.Fprintf(file, z)
            fmt.Fprintf(file, "
")
        }
        defer file.Close()

        elapsed := time.Since(start)
        log.Printf("Execution took %s", elapsed)
    }

}

func isValueInList(list []string, file_string string, read_file []string) []string {

    encountered_modules := make([]string, 0, 10)
    temp_string_filename := ""
    temp_string_filename_counter := 0

    encountered := map[string]bool{}
    result := make([]string, 0, 10)
    final_result := [][]string{}

    for z := range read_file {

        fmt.Println("Reading file n. ", z)

        temp_string_filename = read_file[temp_string_filename_counter]
        f, _ := os.Open(temp_string_filename)
        defer f.Close()

        scanner := bufio.NewScanner(f)
        scanner.Split(bufio.ScanWords)

        for scanner.Scan() {
            line := scanner.Text()
            for _, v := range list {
                if v == line {
                    encountered_modules = append(encountered_modules, line)
                }
            }
        }

        for v := range encountered_modules {
            if encountered[encountered_modules[v]] == true {
                // Do not add duplicate.
            } else {
                // Record this element as an encountered element.
                encountered[encountered_modules[v]] = true
                result = append(result, encountered_modules[v])
            }
        }
        temp_string_filename_counter++
        final_result = append(final_result, result)
    }
    return result

}

func list_file() []string {

    files_names := make([]string, 0, 10)

    files, err := ioutil.ReadDir("./")
    if err != nil {
        log.Fatal(err)
    }

    for _, f := range files {
        if strings.HasSuffix(f.Name(), ".txt") {
            files_names = append(files_names, string(f.Name()))

        }
    }

    return files_names

}

It's hard to be sure, since your code is difficult to read, but this looks particularly suspicious (in pseudocode),

// main
for each file in list_file() {
    result = {
        // isValueInList
        var result
        for each file in list_file() {
            for each word in file {
                if word in wordlist and not in result {
                    result = append(result, word)
                }
            }
        }
        // all the words in wordlist in any of the files
        return result
    }
    // main
    write result
}

There are other problems with your code.


Here's a more readable example (a first draft), of what you appear to be trying to do (Python modules in Python files?):

package main

import (
    "bufio"
    "bytes"
    "fmt"
    "io/ioutil"
    "os"
    "path/filepath"
)

var modules = map[string]bool{
    "numpy": true, "grabscreen": true, "cv2": true, "time": true, "os": true, "pandas": true, "tqdm": true, "collections": true,
    "models": true, "random": true, "inception_v3": true, "googlenet": true, "shuffle": true, "getkeys": true, "tflearn": true,
    "directkeys": true, "statistics": true, "motion": true, "tflearn.layers.conv": true, "conv_2d": true,
    "max_pool_2d": true, "avg_pool_2d": true, "conv_3d": true, "max_pool_3d": true, "avg_pool_3d": true,
}

func findWords(filename string, lexicon map[string]bool) error {
    f, err := os.Open(filename)
    if err != nil {
        return err
    }
    defer f.Close()

    words := make(map[string]bool)

    s := bufio.NewScanner(f)
    s.Split(bufio.ScanWords)
    for s.Scan() {
        word := s.Text()
        if _, exists := lexicon[word]; exists {
            words[word] = true
        }
    }
    if s.Err(); err != nil {
        return err
    }

    var buf bytes.Buffer
    for word := range words {
        buf.WriteString(word)
        buf.WriteString("
")
    }
    if buf.Len() > 0 {
        err := ioutil.WriteFile(filename+`.words`, buf.Bytes(), 0666)
        if err != nil {
            return err
        }
    }
    return nil
}

func main() {
    dir := `./`
    files, err := ioutil.ReadDir(dir)
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
        os.Exit(1)
    }
    for _, file := range files {
        filename := file.Name()
        if filepath.Ext(filename) != ".py" {
            continue
        }
        findWords(filename, modules)
        if err != nil {
            fmt.Fprintln(os.Stderr, err)
        }
    }
}

There are a few mistakes in your code, so i've rewritten most of the code. What i did : 1) open a file 2) read a line 3) compare it 4) check if the target file exists 5) if not, create it 6) if it does, append to it 7) write to it 8) close target file 9) goto 2 if there are more lines 10) goto 1 if there are more files

I tried to make it as much as readable for everybody so that everybody can understand it.

package main

import (
    "bufio"
    "fmt"
    "io/ioutil"
    "log"
    "os"
    "path/filepath"
    "strconv"
    "strings"
    "time"
)

const readDir string = "./"

var startTime time.Time

func main() {
    for noFile, fileName := range listFile() {
        startTime = time.Now()
        fileInput, err := os.Open(fileName)
        if err != nil {
            log.Fatal(err)
        }
        defer fileInput.Close()

        scanner := bufio.NewScanner(fileInput)
        for scanner.Scan() {
            for _, targetContent := range []string{"numpy", "grabscreen", "cv2", "time", "os", "pandas", "tqdm", "collections", "models", "random", "inception_v3", "googlenet", "shuffle", "getkeys", "tflearn", "directkeys", "statistics", "motion", "tflearn.layers.conv", "conv_2d", "max_pool_2d", "avg_pool_2d", "conv_3d", "max_pool_3d", "avg_pool_3d"} {
                if strings.Contains(scanner.Text(), targetContent) {

                    if _, err := os.Stat(readDir + "output_" + strconv.Itoa(noFile)); os.IsNotExist(err) {
                        fmt.Println("File : " + readDir + "output_" + strconv.Itoa(noFile) + " does not exists, creating it now!")
                        createFile, err := os.Create(readDir + "output_" + strconv.Itoa(noFile))
                        if err != nil {
                            panic(err)
                        }
                        createFile.Close()
                    }

                    fileOutput, err := os.OpenFile(readDir+"output_"+strconv.Itoa(noFile), os.O_APPEND|os.O_WRONLY, 0600)
                    if err != nil {
                        panic(err)
                    }

                    if _, err = fileOutput.WriteString("contains : " + targetContent + " in : " + scanner.Text() + "
"); err != nil {
                        panic(err)
                    }

                    fileOutput.Close()

                    fmt.Println("Writing file : ", readDir+"output_"+strconv.Itoa(noFile))
                    fmt.Println("contains : " + targetContent + " in : " + scanner.Text())
                }
            }
        }

        if err := scanner.Err(); err != nil {
            log.Fatal(err)
        }

        log.Printf("Execution took %s", time.Since(startTime))
    }

}

func listFile() []string {

    filesNames := make([]string, 0, 100)

    files, err := ioutil.ReadDir(readDir)
    if err != nil {
        log.Fatal(err)
    }

    for _, f := range files {
        if strings.HasSuffix(f.Name(), ".txt") {
            fileName, err := filepath.Abs(string(f.Name()))
            if err != nil {
                log.Fatal(err)
            }
            filesNames = append(filesNames, fileName)
        }
    }
    return filesNames
}