文件操作
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
951 B

3 years ago
3 years ago
3 years ago
  1. package file
  2. import (
  3. "log"
  4. "os"
  5. "strings"
  6. )
  7. func Substr(s string, pos, length int) string {
  8. runes := []rune(s)
  9. l := pos + length
  10. if l > len(runes) {
  11. l = len(runes)
  12. }
  13. return string(runes[pos:l])
  14. }
  15. func GetParentDirectory(dirctory, SEPARATOR string) string {
  16. currentDir := strings.TrimRight(dirctory, SEPARATOR)
  17. return Substr(currentDir, 0, strings.LastIndex(currentDir, SEPARATOR))
  18. }
  19. /**
  20. * 创建目录
  21. */
  22. func CreatePath(path string) error {
  23. DirStat, err := os.Stat(path)
  24. if err != nil || !DirStat.IsDir() {
  25. err := os.MkdirAll(path, 0755)
  26. if err != nil {
  27. log.Println("ERROR|", path, "目录创建失败", err)
  28. }
  29. }
  30. // _, err := os.Stat(path) //检查目录是否存在
  31. // if err != nil {
  32. // cmd := exec.Command("/bin/sh", "-c", "mkdir -p "+path)
  33. // if runtime.GOOS == "windows" {
  34. // cmd = exec.Command("cmd", "/C", "md "+path)
  35. // } else {
  36. // }
  37. // err := cmd.Run()
  38. // return err
  39. // }
  40. return err
  41. }