package main
import (
"fmt"
"os"
)
// 判断文件夹是否存在
func PathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
func main() {
_dir := "./gzFiles2"
exist, err := PathExists(_dir)
if err != nil {
fmt.Printf("get dir error![%v]\n", err)
return
}
if exist {
fmt.Printf("has dir![%v]\n", _dir)
} else {
fmt.Printf("no dir![%v]\n", _dir)
// 创建文件夹
err := os.Mkdir(_dir, os.ModePerm)
if err != nil {
fmt.Printf("mkdir failed![%v]\n", err)
} else {
fmt.Printf("mkdir success!\n")
}
}
}
// create file with dir if dir is not exist
// path is dir
// name is file name
func createFileWithDir(path string, name string, content string) {
os.MkdirAll(path, os.ModePerm)
file, _ := os.OpenFile(path + "/" + name, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
defer file.Close()
file.WriteString(content)
}