2020年11月6日 星期五

[ 文章收集 ] List the files in a folder with Go

 Source From Here

Preface
In this article I explain how to get a list of files inside a folder on the filesystem, a task also called tree traversing, with Go. There are two handy stdlib functions that you can use depending on your goal. I list 3 ways: using filepath.Walkioutil.ReadDir or os.File.Readdir.

Using filepath.Walk
The path/filepath stdlib package provides the handy Walk function. It automatically scans subdirectories, though, so make sure this is what you actually want.

Usage is very simple:
  1. package main  
  2.   
  3. import (  
  4.     "fmt"  
  5.     "os"  
  6.     "path/filepath"  
  7. )  
  8.   
  9. func main() {  
  10.     var files []string  
  11.   
  12.     root := "/some/folder/to/scan"  
  13.     err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {  
  14.         files = append(files, path)  
  15.         return nil  
  16.     })  
  17.     if err != nil {  
  18.         panic(err)  
  19.     }  
  20.     for _, file := range files {  
  21.         fmt.Println(file)  
  22.     }  
  23. }  
filepath.Walk accepts a string pointing to the root folder, and a WalkFunc, a function type with signature:
  1. type WalkFunc func(path string, info os.FileInfo, err error) error  
This function is called for each iteration of the folder scan.

The info variable of type os.FileInfo is important because we can get many information on the current file: the name, size, mode, modification time, if it’s a folder or a file, and the underlying data source.

For example we could avoid processing folders by adding:
  1. if info.IsDir() {  
  2.     return nil  
  3. }  
You can exclude (or include) files to the slice based on their extension, by using filepath.Ext and passing the file path:
  1. if filepath.Ext(path) == ".dat" {  
  2.     return nil  
  3. }  
We could store the file name instead of the file path using:
  1. files = append(files, info.Name())  
And we could also define the WalkFunc in a separate closure. We just need to pass a pointer to files in visit:
  1. package main  
  2.   
  3. import (  
  4.     "fmt"  
  5.     "log"  
  6.     "os"  
  7.     "path/filepath"  
  8. )  
  9.   
  10. func visit(files *[]string) filepath.WalkFunc {  
  11.     return func(path string, info os.FileInfo, err error) error {  
  12.         if err != nil {  
  13.             log.Fatal(err)  
  14.         }  
  15.         *files = append(*files, path)  
  16.         return nil  
  17.     }  
  18. }  
  19.   
  20. func main() {  
  21.     var files []string  
  22.   
  23.     root := "/some/folder/to/scan"  
  24.     err := filepath.Walk(root, visit(&files))  
  25.     if err != nil {  
  26.         panic(err)  
  27.     }  
  28.     for _, file := range files {  
  29.         fmt.Println(file)  
  30.     }  
  31. }  
Using ioutil.ReadDir
filepath.Walk is handy but scans subfolders too, by default, which might not be what you want.

The Go stdlib also provides ioutil.ReadDir
  1. func ReadDir(dirname string) ([]os.FileInfo, error)  
ReadDir reads the directory named by dirname and returns a list of directory entries sorted by filename.

and here’s an example from the docs. ioutil.ReadDir takes a folder path as a string and returns a slice of os.FileInfo, which we described above.
  1. package main  
  2.   
  3. import (  
  4.     "fmt"  
  5.     "io/ioutil"  
  6.     "log"  
  7. )  
  8.   
  9. func main() {  
  10.     files, err := ioutil.ReadDir(".")  
  11.     if err != nil {  
  12.         log.Fatal(err)  
  13.     }  
  14.   
  15.     for _, file := range files {  
  16.         fmt.Println(file.Name())  
  17.     }  
  18. }  
Using os.File.Readdir
Internally, ioutil.ReadDir is implemented as:
  1. // ReadDir reads the directory named by dirname and returns  
  2. // a list of directory entries sorted by filename.  
  3. func ReadDir(dirname string) ([]os.FileInfo, error) {  
  4.     f, err := os.Open(dirname)  
  5.     if err != nil {  
  6.         return nil, err  
  7.     }  
  8.     list, err := f.Readdir(-1)  
  9.     f.Close()  
  10.     if err != nil {  
  11.         return nil, err  
  12.     }  
  13.     sort.Slice(list, func(i, j int) bool { return list[i].Name() < list[j].Name() })  
  14.     return list, nil  
  15. }  
as you can see it scans dirname and sorts the files by name. If you don’t need sorting you can as well use:
  1. package main  
  2.   
  3. import (  
  4.     "fmt"  
  5.     "log"  
  6.     "os"  
  7. )  
  8.   
  9. func main() {  
  10.     dirname := "."  
  11.   
  12.     f, err := os.Open(dirname)  
  13.     if err != nil {  
  14.         log.Fatal(err)  
  15.     }  
  16.     files, err := f.Readdir(-1)  
  17.     f.Close()  
  18.     if err != nil {  
  19.         log.Fatal(err)  
  20.     }  
  21.   
  22.     for _, file := range files {  
  23.         fmt.Println(file.Name())  
  24.     }  
  25. }  


沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...