dongzheng7165 2015-10-16 13:40
浏览 77
已采纳

Go中的Filepath.Walk无法在Windows下正确提取C:驱动器下的文件夹

I am using the Filepath.Walk in Go to try to get all the folders under C: recursively. However it just returns me sub-folders starting with $Recycle.Bin. What am I doing wrong here?

package main

import (
    "fmt"
    "path/filepath"
    "os"
)

func main() {
    array := Subfolders("C:")
    for _,value := range array {
        fmt.Println(value)
    }
}

func Subfolders(path string) (paths []string) {
    filepath.Walk(path, func(newPath string, info os.FileInfo, err error) error {
        if err != nil {
            return err
        }
        if info.IsDir() {
            paths = append(paths, newPath)
        }
        return nil
    })  
    return paths
}

Output:
C:
C:\$Recycle.Bin
C:\$Recycle.Bin\S-1-5-21-310629982-3373693989-3733510080-1000
C:\$Recycle.Bin\S-1-5-21-310629982-3373693989-3733510080-1000\$R0L9M20
C:\$Recycle.Bin\S-1-5-21-310629982-3373693989-3733510080-1000\$R22ZOD9
C:\$Recycle.Bin\S-1-5-21-310629982-3373693989-3733510080-1000\$R3LS9P4
C:\$Recycle.Bin\S-1-5-21-310629982-3373693989-3733510080-1000\$R4T2IGU
C:\$Recycle.Bin\S-1-5-21-310629982-3373693989-3733510080-1000\$R8TZIET
C:\$Recycle.Bin\S-1-5-21-310629982-3373693989-3733510080-1000\$R9QQZB9
C:\$Recycle.Bin\S-1-5-21-310629982-3373693989-3733510080-1000\$RA71HY3
C:\$Recycle.Bin\S-1-5-21-310629982-3373693989-3733510080-1000\$RBOC0V4.com
C:\$Recycle.Bin\S-1-5-21-310629982-3373693989-3733510080-1000\$RBOC0V4.com\go-fsnotify
C:\$Recycle.Bin\S-1-5-21-310629982-3373693989-3733510080-1000\$REJFS3Z
C:\$Recycle.Bin\S-1-5-21-310629982-3373693989-3733510080-1000\$RF9A1Y6
C:\$Recycle.Bin\S-1-5-21-310629982-3373693989-3733510080-1000\$ROMESWQ
C:\$Recycle.Bin\S-1-5-21-310629982-3373693989-3733510080-1000\$RP4CYID
C:\$Recycle.Bin\S-1-5-21-310629982-3373693989-3733510080-1000\$RQHMPV5
C:\$Recycle.Bin\S-1-5-21-310629982-3373693989-3733510080-1000\$RV0K99H
C:\$Recycle.Bin\S-1-5-21-310629982-3373693989-3733510080-1000\$RX54T04

I have many other folders under C: which donot get picked up by the filepath.walk. I wanted to understand what was the reason behind it.

EDIT


Thanks to the answers I was able to resolve the issue as below:-

func Subfolders(path string) (paths []string) {
    filepath.Walk(path, func(newPath string, info os.FileInfo, err error) error {
        if err != nil {
            log.Println(err)
            return filepath.SkipDir
        }

        if info.IsDir() {
            paths = append(paths, newPath)
        }
        return nil
    })
  • 写回答

2条回答 默认 最新

  • donglu7998 2015-10-16 13:44
    关注

    You're blindly returning errors without logging them. Returning a non-nil error from the callback is a signal for filepath.Walk to abort.

    Presumably there is some file you don't have access to, or something.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?