Golang实现判断文件或文件夹是否存在
golang通过使用 os
包中的Stat()
函数和IsNotExist()
函数即可判断文件或文件夹是否存在。
package main
import (
"os"
"fmt"
)
func main(){
filepath := "C:\\Users\\Administrator\\Desktop\\user_agent.py"
_,err := os.Stat(filepath)
if os.IsNotExist(err){
fmt.Println("file not exist")
return
}
fmt.Println("file exist")
}
转载声明
本文版权归作者所有
如需转载,请注明出处;本文地址: https://www.perfcode.com/p/1546.html