文件操作
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.

50 lines
938 B

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