Last active
August 29, 2015 14:16
-
-
Save indraniel/310366787ea167c9b9db to your computer and use it in GitHub Desktop.
check if a file exists on the file system
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// approach 1 | |
if _, err := os.Stat("/path/to/whatever"); os.IsNotExist(err) { | |
// path/to/whatever does not exist | |
} | |
// approach 2 | |
if _, err := os.Stat("/path/to/whatever"); err == nil { | |
// path/to/whatever exists | |
} | |
// equivalent to Python's `if os.path.exists(filename)` | |
if _, err := os.Stat(filename); err == nil { | |
fmt.Printf("file exists; processing...") | |
process(filename) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment