如何高效地在所有目录中找到与特定后缀匹配的所有文件?

huangapple go评论75阅读模式
英文:

How to efficiently find all the files matching a particular suffix in all the directories?

问题

我需要递归地在一个目录中查找所有的文件,这个目录可能包含很多子目录。我想知道是否有办法只获取 *.json 格式的文件?我有下面这个方法可以完成任务,并且可以递归地获取所有目录中的文件,但是我不确定在这里使用 HasSuffix 是否正确。我对golang还不熟悉,所以不确定是否有更好或更高效的方法来实现这个功能。

func WalkDir(root string) ([]string, error) {
    var files []string
    err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
        if !info.IsDir() && strings.HasSuffix(path, "json") {
            files = append(files, path)
        }
        return nil
    })
    return files, err
}

另外,我该如何修改上面的方法,以便它可以给我所有匹配 *.json*.txt 后缀的文件。基本上,它应该能够使用后缀数组并给我所有匹配的文件。有什么高效的方法可以实现这个功能?

英文:

I need to recursively find all the files in a directory which can have lot of sub-directories too. I was wondering if there is any way it can only give me *.json files only?
I have below method which does the job and gives me all files recursively in all the directories but I am not sure whether I am doing it right by using HasSuffix here. I am new to golang so I not sure if there is any better or efficient way to do this.

func WalkDir(root string) ([]string, error) {
	var files []string
	err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
		if !info.IsDir() && strings.HasSuffix(path, "json") {
			files = append(files, path)
		}
		return nil
	})
	return files, err
}

Also how can I modify above method so that it can give me all the files matching *.json or *.txt suffix. Basically it should be able to work with suffix array and give me all files matching that. What is the efficient way to do this?

答案1

得分: 4

你可能希望确保文件后缀之前有一个点(.)。

另外,正如@maxm指出的那样,根据文档,更推荐使用WalkDir

> Walk比WalkDir效率低,WalkDir是在Go 1.16中引入的,它避免了在访问每个文件或目录时调用os.Lstat。

要检查多个后缀的匹配,你可以尝试以下代码:

func WalkDir(root string, exts []string) ([]string, error) {
    var files []string
    err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
        if d.IsDir() {
            return nil
        }

        for _, s := range exts {
            if strings.HasSuffix(path, "."+s) {
                files = append(files, path)
                return nil
            }
        }

        return nil
    })
    return files, err
}

使用方法如下:

files, err := WalkDir("/home", []string{"json", "txt"})
英文:

You probably want to ensure the dot (.) is present before the file suffix too.

Also as noted by @maxm, WalkDir is preferred according to the docs:

> Walk is less efficient than WalkDir, introduced in Go 1.16, which
> avoids calling os.Lstat on every visited file or directory.

To check for a match of multiple suffixes you can try:

func WalkDir(root string, exts []string) ([]string, error) {
    var files []string
    err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
        if d.IsDir() {
            return nil
        }

        for _, s := range exts {
            if strings.HasSuffix(path, "."+s) {
                files = append(files, path)
                return nil
            }
        }

        return nil
    })
    return files, err
}

And to use:

files, err := WalkDir("/home", []string{"json", "txt"})

huangapple
  • 本文由 发表于 2021年12月31日 08:34:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/70537979.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定