FileInfo.IsDir()无法检测到目录。

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

FileInfo.IsDir() isn't detecting directory

问题

我有一些代码,它遍历一个目录以获取文件,并对它们进行操作,它使用IsDir()函数来跳过目录。然而,一个目录没有被正确地检测到:

err = filepath.Walk(dir, func(path string, f os.FileInfo, err error) error {
    if !f.IsDir() {
        fileList = append(fileList, path)
    }
    return nil
})

错误信息:

Put app/javascripts: read public/app/javascripts: is a directory

我在OSX上。这是目录列表:

drwxr-xr-x@   6 me  staff     204 Sep 25 11:28 javascripts

有没有更好的方法来检测目录?

英文:

I have some code that walks through a directory to get the files, to operate on them and it uses IsDir() to skip directories. However, a directory isn't properly being detected:

err = filepath.Walk(dir, func(path string, f os.FileInfo, err error) error {
	if !f.IsDir() {
		fileList = append(fileList, path)
	}
	return nil
})

Error:

Put app/javascripts: read public/app/javascripts: is a directory

I'm on OSX. Here's the directory listing:

drwxr-xr-x@   6 me  staff     204 Sep 25 11:28 javascripts

Is there a better way to detect directories?

答案1

得分: 1

我之前看错了目录(名字有点混淆)。原来它是一个符号链接,这就说得通了。

英文:

I was looking at the wrong dir (confusing names). It was a symlink, which makes sense.

答案2

得分: 1

看起来该文件既是一个目录又是一个符号链接。为了排除同时满足这两个条件的文件,你可以使用以下代码:

if !f.IsDir() && (f.Mode()&os.ModeSymlink) != os.ModeSymlink {
    ...
}
英文:

It looks like that file is a directory and also a symlink. In order to exclude files which match both conditions, you can use:

if !f.IsDir() && (f.Mode()&os.ModeSymlink) != os.ModeSymlink {
    ...
}

huangapple
  • 本文由 发表于 2015年10月2日 21:29:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/32908277.html
匿名

发表评论

匿名网友

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

确定