drxv39706 2015-07-30 16:28
浏览 224
已采纳

Go-遍历当前目录中的目录/文件

I have the following structure:

project/
    docs/
        index.html
    root.html

I'm trying to iterate through this project structure so that I can read the contents of each file to process them. So I want to say "search through the directory project", then it will search through all the files, and only the first level of directories and their files, so if there was another directory with a file inside of docs/, it would ignore it.

Currently, I've tried to accomplish this with the "path/filepath" library:

func traverse(path string, file os.FileInfo, err error) error {
    if file, err := os.Open(file.Name()); err == nil {
        defer file.Close()
        if fileStat, err := file.Stat(); err == nil {
            switch mode := fileStat.Mode(); {
            case mode.IsDir():
                fmt.Println("it be a directory! lets traverse", file.Name())
                filepath.Walk(file.Name(), traverse)
            case mode.IsRegular():
                fmt.Println("the thingy ", file.Name(), " is a file")
            }
        } else {
            return errors.New("failed to check status")
        }
    }
    return errors.New("failed 2 open file/dir?")
}

Then I call it from here:

if err := filepath.Walk("project/", traverse); err != nil {
    setupErr("%s", err)
}

Note that I run this executable relative to my test directory, so it's finding the directory okay. My problem is actually when I run it, I get the following:

it be a directory! lets traverse project
it be a directory! lets traverse project
# ^ printed about 20 more times ^
failed 2 open file/dir?

I think my recursion is a little off, and it's not changing into the directory perhaps? Any ideas, if you need any more information just ask and I'll update.

  • 写回答

2条回答 默认 最新

  • dongshen3352 2015-07-30 16:44
    关注

    First, it looks like what you want do to contradicts with the code you have. You wrote:

    So I want to say "search through the directory project", then it will search through all the files, and only the first level of directories and their files, so if there was another directory with a file inside of docs/, it would ignore it.

    Does it mean that you want to iterate only two levels of directories (current and one below) and ignore the rest?

    If so then you do not need a recursion, just a simple loop that executes search function over the files within the current directory and for every its subdirectory.

    The code that you have walks over the filesystem directory subtree.

    Basically, filepath.Walk that you use should do it for you. So you either implement recursive walking or use Walk, but not both.

    Second, the recursion is implemented incorrectly in your code. It missing iterating over the directories.

    So the code that prints the file names in the current directory and its subdirectories (but not further) is:

    package main
    
    import (
        "fmt"
        "io/ioutil"
    )
    
    func main() {
        items, _ := ioutil.ReadDir(".")
        for _, item := range items {
            if item.IsDir() {
                subitems, _ := ioutil.ReadDir(item.Name())
                for _, subitem := range subitems {
                    if !subitem.IsDir() {
                        // handle file there
                        fmt.Println(item.Name() + "/" + subitem.Name())
                    }
                }
            } else {
                // handle file there
                fmt.Println(item.Name())
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办