douquan1015
2017-07-16 06:00
浏览 253
已采纳

如何检查文件名(带有任何扩展名)在Golang目录中是否存在?

I know I could check the file does exist or not in Golang by the answers of the following questions.

The code looks like this.

_, err := os.Stat(path)
if err == nil {
    log.Printf("File %s exists.", path)
} else if os.IsNotExist(err) {
    log.Printf("File %s not exists.", path)
} else {
    log.Printf("File %s stat error: %v", path, err)
}

But here's my real question, how do I check the filename does exist (has been used) in the specified directory? For example if I have a file tree like this:

--- uploads
       |- foo.png
       |- bar.mp4

I wanted to check if there's any file is using the specified name..

used := filenameUsed("uploads/foo")
fmt.Println(used) // Output: true

used = filenameUsed("uploads/hello")
fmt.Println(used) // Output: false

How do I implement the filenameUsed function?

Google gave me a path/filepath package as the result but I have no clue about how to use it.

图片转代码服务由CSDN问答提供 功能建议

我知道可以通过以下问题的答案来检查Golang中文件是否存在。

  • 如何检查Golang中是否存在由路径表示的文件或目录?
  • 如何检查Go中是否存在文件?
  • < a href =“ https://gist.github.com/mattes/d13e273314c3b3ade33f” rel =“ nofollow noreferrer”>要点-检查文件或目录是否存在于Golang中 \ n

    代码看起来像这样。

      _,err:= os.Stat(path)
    if err == nil {
     log.Printf(  “文件%s存在。”,路径)
    }否则,如果os.IsNotExist(err){
     log.Printf(“文件%s不存在。”,路径)
    }否则{
     log.Printf(  “文件%s stat错误:%v”,路径,错误)
    } 
       
     
     

    但是这里' 我的真正问题是,如何检查指定目录中的文件名是否存在(已使用 )? 例如,如果我有一个像这样的文件树:

      ---上传
     |-foo.png 
     |-bar.mp4 
      <  / pre> 
     
     

    我想检查是否有任何文件正在使用指定的名称。.

      used:= filenameUsed(“ uploads / foo”)  
    fmt.Println(使用)//输出:true 
     
    used = filenameUsed(“ uploads / hello”)
    fmt.Println(使用)//输出:false 
       
     \  n 

    如何实现 filenameUsed 函数?

    Google给了我 path / filepath 包作为结果,但是我不知道如何使用它。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dpyoh6553 2017-07-16 06:35
    已采纳

    You may use the filepath.Glob() function where you can specify a pattern to list files.

    The pattern to be used is basically the name you wish to check if used, extended with the any extension pattern.

    Example:

    func filenameUsed(name string) (bool, error) {
        matches, err := filepath.Glob(name + ".*")
        if err != nil {
            return false, err
        }
        return len(matches) > 0, nil
    }
    

    Using / testing it:

    fmt.Print("Filename foo used: ")
    fmt.Println(filenameUsed("uploads/foo"))
    fmt.Print("Filename bar used: ")
    fmt.Println(filenameUsed("uploads/bar"))
    

    Example output:

    Filename foo used: true <nil>
    Filename bar used: false <nil>
    

    However, note that filenameUsed() returning false (and nil error) does not mean a file with that name won't exist if you attempt to create one after. Meaning checking it and attempting to create such a file does not guarantee atomicity. If your purpose is to create a file if the name is not used, then simply try to create the file in the proper mode (do not overwrite if exists), and handle the (creation) error returned by that call.

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题