-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdir.go
More file actions
65 lines (59 loc) · 1.35 KB
/
Copy pathdir.go
File metadata and controls
65 lines (59 loc) · 1.35 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* @Author: leafney
* @GitHub: https://github.com/leafney
* @Project: rose
* @Date: 2024-04-24 08:33
* @Description:
*/
package rose
import (
"os"
"path/filepath"
)
// DirExists 判断目录路径是否存在
func DirExists(path string) bool {
// 获取路径的状态信息
info, err := os.Stat(path)
if err != nil {
// 如果路径不存在或发生其他错误
if os.IsNotExist(err) {
return false
}
return false
}
// 判断是否为目录
return info.IsDir()
}
// Deprecated: use DirExists replaced
// DIsExist 判断目录路径是否存在
func DIsExist(path string) bool {
exist, err := DIsExistE(path)
if err != nil {
return false
}
return exist
}
// Deprecated: use DirExists replaced
// DIsExistE 判断目录路径是否存在,抛出异常
func DIsExistE(path string) (bool, error) {
dirPath := filepath.Dir(path)
info, err := os.Stat(dirPath)
if os.IsNotExist(err) {
return false, nil
}
if err != nil {
return false, err
}
return info.IsDir(), nil
}
// DirExistsEnsure 确保目录路径存在 (判断路径是否存在,如果不存在则自动创建所需目录路径
func DirExistsEnsure(path string) error {
dirPath := filepath.Dir(path)
if _, err := os.Stat(dirPath); os.IsNotExist(err) {
err := os.MkdirAll(dirPath, os.ModePerm)
if err != nil {
return err
}
}
return nil
}