英文:
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 {
...
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论