Skip to content

Instantly share code, notes, and snippets.

@mattes

mattes/check.go

Last active Oct 16, 2020
Embed
What would you like to do?
Check if file or directory exists in Golang
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
}
@billzhuang

This comment has been minimized.

Copy link

@billzhuang billzhuang commented Mar 17, 2016

nice to know

@luismanolo

This comment has been minimized.

Copy link

@luismanolo luismanolo commented Jul 28, 2016

Thanks.

@anonrose

This comment has been minimized.

Copy link

@anonrose anonrose commented Aug 18, 2016

Thank you

@cn27001

This comment has been minimized.

Copy link

@cn27001 cn27001 commented Sep 14, 2016

ths

@lccezinha

This comment has been minimized.

Copy link

@lccezinha lccezinha commented Oct 6, 2016

thanks!

@Sag0Sag0

This comment has been minimized.

Copy link

@Sag0Sag0 Sag0Sag0 commented Jun 7, 2017

Thanks.

@xhesh

This comment has been minimized.

Copy link

@xhesh xhesh commented Jul 4, 2017

Thank you

@yshadow

This comment has been minimized.

Copy link

@yshadow yshadow commented Aug 17, 2017

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.

@shinriyo

This comment has been minimized.

Copy link

@shinriyo shinriyo commented Sep 10, 2017

fmt.Printf("Error: %s", err)

@cuixin

This comment has been minimized.

Copy link

@cuixin cuixin commented Nov 17, 2017

@yshadow, absolutely right!

@kshitij10496

This comment has been minimized.

Copy link

@kshitij10496 kshitij10496 commented Dec 20, 2017

@sunicy

This comment has been minimized.

Copy link

@sunicy sunicy commented Jan 2, 2018

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
}
@JitinGuglani

This comment has been minimized.

Copy link

@JitinGuglani JitinGuglani commented Apr 23, 2018

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"
}

@SteelCrow

This comment has been minimized.

Copy link

@SteelCrow SteelCrow commented Aug 24, 2018

Backslash \ is escape character, that's why I think you getting this strange behaviour.

@daqing613

This comment has been minimized.

Copy link

@daqing613 daqing613 commented Jan 22, 2019

Thanks.

@ilyaiqoqo

This comment has been minimized.

Copy link

@ilyaiqoqo ilyaiqoqo commented Jun 5, 2019

like!

@danawoodman

This comment has been minimized.

Copy link

@danawoodman danawoodman commented Oct 19, 2019

Why not use os.IsExist instead of !os.IsNotExist?

@mattes

This comment has been minimized.

Copy link
Owner Author

@mattes mattes commented Oct 21, 2019

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
}
@digglife

This comment has been minimized.

Copy link

@digglife digglife commented Apr 13, 2020

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.

@rickyzhang82

This comment has been minimized.

Copy link

@rickyzhang82 rickyzhang82 commented May 6, 2020

@digglife Welcome to this duck up Golang world.

@felts94

This comment has been minimized.

Copy link

@felts94 felts94 commented Aug 18, 2020

👍

@ravindrabhargava

This comment has been minimized.

Copy link

@ravindrabhargava ravindrabhargava commented Sep 20, 2020

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");
}

@mattes

This comment has been minimized.

Copy link
Owner Author

@mattes mattes commented Sep 21, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.