-
-
Save mattes/d13e273314c3b3ade33f to your computer and use it in GitHub Desktop.
if _, err := os.Stat("/path/to/whatever"); os.IsNotExist(err) { | |
// path/to/whatever does not exist | |
} | |
if _, err := os.Stat("/path/to/whatever"); !os.IsNotExist(err) { | |
// path/to/whatever exists | |
} |
Thanks.
Thank you
The condition for checking if the folder / file exist is not exactly correct. os.Stat might fail because of permissions, etc, but the file may still exists. So it should be !os.IsNotExist(err). Using err == nil in that case gives wrong result as the file doesn't exist.
fmt.Printf("Error: %s", err)
@yshadow, absolutely right!
Reference: https://stackoverflow.com/a/12518877/4927751
One more thing, os.IsExist() would be blind to EMPTY FILE. Please always consider IsNotExist() instead.
Here is the official implementation of both of the function.
func isExist(err error) bool {
err = underlyingError(err)
return err == syscall.EEXIST || err == syscall.ENOTEMPTY || err == ErrExist
}
func isNotExist(err error) bool {
err = underlyingError(err)
return err == syscall.ENOENT || err == ErrNotExist
}
Hi all,
very funny behavior of os.stat(). I have written the below code and os.stat() is behaving very wierd.
I am using windows and go lang version : go1.10.1 windows/amd64.
Pls help.
package main
import "os"
func main() {
directoryName := getName()
_, err := os.Stat(directoryName)
if err != nil {
println("os.Stat(): error for folder name ",directoryName)
println("and error is : ", err.Error())
if os.IsNotExist(err ) {
println("Directory Does not exists.")
}
} else {
println("os.Stat(): No Error for folderName : ", directoryName)
}
}
// none of these folder exists on my computer so ideally all the returns statements should give error, but only some are giving.
func getName() string {
//no error if I return the "d:\invalidFolder" value
//return "d:\invalidFolder"
//error if I return the "d:\InvalidFolderName" value
//return "d:\InvalidFolderName"
// error if I return the "d:\xyz\this\folder" value
//return "d:\xyz\this\folder"
// no error if I return the "d:\xyz" value
//return "d:\xyz"
// error, if I return the "d:\doesitgiveError" value
//return "d:\doesitgiveError"
// error if I return "d:\abc" value
return "d:\abc"
}
Backslash \ is escape character, that's why I think you getting this strange behaviour.
Thanks.
like!
Why not use os.IsExist
instead of !os.IsNotExist
?
Here is an example to explain the difference, why os.IsExist != !os.IsNotExist
:
if _, err := os.Stat("/path/to/whatever"); os.IsExist(err) {
// never triggers, because err is nil if file actually exists
}
if err := os.Mkdir("/path/to/whatever", 0755); os.IsExist(err) {
// triggers if dir already exists
}
Is this normal? Have anyone here ever typed this long to just test if a file/dir exists in any other modern language?
Thanks for the solution tho.
@digglife Welcome to this duck up Golang world.
👍
what about if need to check for "*.txt" in Linux dir if the file exists or not, I tried *.txt but it's not working at all
if _, err := os.Stat("*.txt"); err == nil {
fmt.Printf("File exists\n");
} else {
fmt.Printf("File does not exist\n");
}
@ravindrabhargava Please have a look at https://golang.org/pkg/path/filepath/#Glob
super!
Great! It was so helpful! 👍
Thanks 👍
Thanks
that helps~
Thanks
I was here.
Thanks
Thanks!
shanks!
haha, although it is trivial still helps a lot ,thanks!
Thanks
thanks!