golang判断文件或文件夹是否存在 文件/文件夹是否存在 /** * function 判断文件/文件夹是否存在 * param path: 文件/文件夹的路径 * return bool:true存在,false不存在 * error:存在返回nil,不存在返回错误 */ func FileAndDirIsExistCommonService(path string) (bool, error) { fileInfo, erByStat := os.Stat(path) if erByStat != nil { logs.Error("os stat %s error......%s", path, erByStat.Error()) //该判断主要是部分文件权限问题导致os.Stat()出错,具体看业务启用 //使用os.IsNotExist()判断为true,说明文件或文件夹不存在 //if os.IsNotExist(erByStat) { // logs.Error("%s is not exist", erByStat.Error()) // return false, erByStat //}else{ //文件/文件夹存在 //return true, nil // } return false, erByStat } //打印名称 fmt.Printf("File/Dir name=%s", fileInfo.Name()) return true, nil } 有些代码会使用==os.Open==来完成上述工作; 不过最好不要这么做,因为虽然两者完成的功能没有区别; 但在调用开销方面,stat小于open; 而且对于判断文件是否存在; 检查它的元数据要比直接尝试打开它更加合理
jshgtfrddh2年前0
你的文章让我感受到了不一样的视角,非常精彩。 https://www.yonboz.com/video/84282.htmlukfbunfxqk2年前0
你的文章让我感受到了不一样的视角,非常精彩。 https://www.yonboz.com/video/23752.htmldmtrqccbnn2年前0
你的文章内容非常卖力,让人点赞。 http://www.55baobei.com/l1t7Yd5s6H.htmlxsuhtbbzmm2年前0
你的文章让我感受到了不一样的视角,非常精彩。 https://www.4006400989.com/qyvideo/92199.htmlceskjhtfrd2年前0
你的文章内容非常卖力,让人点赞。 http://www.55baobei.com/l1t7Yd5s6H.html