1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
package os
import gos "os"
import "path/filepath"
// Delete a tree of empty directories. Returns non-nil error if
// an empty directory cannot be deleted. Does not return an error
// if there are non-empty directories. This function can be used
// to prune empty directories from a tree.
func RemoveEmpty(path string) error {
var dirs []string
return filepath.Walk(path, func(path string, info gos.FileInfo, err error) error {
if info.Mode().IsDir() {
dirs = append(dirs, path)
}
return nil
})
for i := len(dirs) - 1; i >= 0; i-- {
err := gos.Remove(dirs[i])
if err != nil && !IsNotEmpty(err) {
return err
}
}
return nil
}
|