How to check if a file exists in Go?

前端 未结 11 1748
陌清茗
陌清茗 2020-11-30 16:23

Go\'s standard library does not have a function solely intended to check if a file exists or not (like Python\'s os.path.exists). What is the idiomatic way

相关标签:
11条回答
  • 2020-11-30 16:49

    basicly

    
    package main
    
    import (
        "fmt"
        "os"
    )
    
    func fileExists(path string) bool {
        _, err := os.Stat(path)
        return !os.IsNotExist(err)
    }
    
    func main() {
    
        var file string = "foo.txt"
        exist := fileExists(file)
        
        if exist {
            fmt.Println("file exist")
        } else {
    
            fmt.Println("file not exists")
        }
    
    }
    
    

    run example

    other way

    with os.Open

    package main
    
    import (
        "fmt"
        "os"
    )
    
    func fileExists(path string) bool {
        _, err := os.Open(path) // For read access.
        return err == nil
    
    }
    
    func main() {
    
        fmt.Println(fileExists("d4d.txt"))
    
    }
    
    
    

    run it

    0 讨论(0)
  • 2020-11-30 16:52

    The example by user11617 is incorrect; it will report that the file exists even in cases where it does not, but there was an error of some other sort.

    The signature should be Exists(string) (bool, error). And then, as it happens, the call sites are no better.

    The code he wrote would better as:

    func Exists(name string) bool {
        _, err := os.Stat(name)
        return !os.IsNotExist(err)
    }
    

    But I suggest this instead:

    func Exists(name string) (bool, error) {
      _, err := os.Stat(name)
      if os.IsNotExist(err) {
        return false, nil
      }
      return err != nil, err
    }
    
    0 讨论(0)
  • 2020-11-30 16:53
        _, err := os.Stat(file)
        if err == nil {
            log.Printf("file %s exists", file)
        } else if os.IsNotExist(err) {
            log.Printf("file %s not exists", file)
        } else {
            log.Printf("file %s stat error: %v", file, err)
        }
    
    0 讨论(0)
  • 2020-11-30 16:53

    Let's look at few aspects first, both the function provided by os package of golang are not utilities but error checkers, what do I mean by that is they are just a wrapper to handle errors on cross platform.

    So basically if os.Stat if this function doesn't give any error that means the file is existing if it does you need to check what kind of error it is, here comes the use of these two function os.IsNotExist and os.IsExist.

    This can be understood as the Stat of the file throwing error because it doesn't exists or is it throwing error because it exist and there is some problem with it.

    The parameter that these functions take is of type error, although you might be able to pass nil to it but it wouldn't make sense.

    This also points to the fact that IsExist is not same as !IsNotExist, they are way two different things.

    So now if you want to know if a given file exist in go, I would prefer the best way is:

    if _, err := os.Stat(path/to/file); !os.IsNotExist(err){
       //TODO
    } 
    
    0 讨论(0)
  • 2020-11-30 16:57

    The function example:

    func file_is_exists(f string) bool {
        _, err := os.Stat(f)
        if os.IsNotExist(err) {
            return false
        }
        return err == nil
    }
    
    0 讨论(0)
提交回复
热议问题